Tuesday 25 December 2012

Convert Html control to string

eg: In server side, through coding, we have created a HTMLAnchor. Adding this to a Label's text requires in string format.
 
private static string GetControlsAsString(Control ulControl)
{
 string contents = null;
 using (System.IO.StringWriter swriter = new System.IO.StringWriter())
 {
 HtmlTextWriter writer = new HtmlTextWriter(swriter);
 ulControl.RenderControl(writer);
 contents = swriter.ToString();
 }
 return contents;
}

Gets thumbnail, full, large size url images from a sharepoint picture library


///Provides the url to a picture provided the picture library list item.
private static string GetPictureUrl(SPListItem listItem, ImageSize imageSize)
{
 StringBuilder url = new StringBuilder();

 // Build the url up until the final portion
 url.Append(SPEncode.UrlEncodeAsUrl(listItem.Web.Url));
 url.Append('/');
 url.Append(SPEncode.UrlEncodeAsUrl(listItem.ParentList.RootFolder.Url));
 url.Append('/');

 // Determine the final portion based on the requested image size
 string filename = listItem.File.Name;

  if (imageSize == ImageSize.Full)
 {
   url.Append(SPEncode.UrlEncodeAsUrl(filename));
 }
 else
 {
 string basefilename = Path.GetFileNameWithoutExtension(filename);
 string extension = Path.GetExtension(filename);
 string dir = (imageSize == ImageSize.Thumbnail) ? "_t/" : "_w/";  url.Append(dir);
 url.Append(SPEncode.UrlEncodeAsUrl(basefilename));
 url.Append(SPEncode.UrlEncodeAsUrl(extension).Replace('.', '_'));
 url.Append(".jpg");
 }
return url.ToString();
}

 // Enum
public enum ImageSize
{
 Thumbnail,
 Full,
 Large
}

Monday 24 December 2012

Configure Advance Search to limit to a scope

I have read many articles to configure Advance Search webpart, but i want to search those items from a particular list, document library, site etc.,
I have faced many hurdles in limiting to those scopes, so i want to post this as other developers or admins can easily refer this and implement on their own.




To provide the end user with advance search, 3 steps need to be followed.



Step 1: Create Scopes

Go to 'Central Administration' → Shares Service Provider(SSP) → 'Search Settings' under search → 'View Scopes' under 'Scopes'.

Click on 'New Scope'.






Provide Title, Description. Click on ok.

Next step is to create rules.

Click on newly created scope → New Rule → provide 'Scope Rule Type'.
Eg.,

1. If to limit to a sharepont list, type the url of the list:

2. If to limit to a Site:

Click on Ok!!



Step 2: Create Display groups

Type the following url:

It will be displaying the existing scopes and 'Display groups'.
Based on requirement, create a new display group or edit the existing one.




Click on 'New Display group', type the Title, Description, scopes to be included, and the default scope.
Click on Ok.


Step 3: Add Webpart

On any custom page, Add out-of-the-box webpart 'Advance Search Box' under 'Search' category.



Go to webpart properties:

I. Under 'Scopes' section, provide the 'Display group' name.


  1. Under 'Miscellaneous' section, provide the url to redirect the page on click of 'Search' button.


    Click on ok!!

    The provided url will be a custom .aspx page which has the Out-Of-the-box webpart 'Search Core Results'. No need to do any settings for this page.


    Done.

    References: