Thursday, December 13, 2012

Gems in Sharepoint

1.How to get the  default SharePoint installation path?
Some times we have a requirement to get the folder path of 12/14 hive and most of the time we end up in hardcoding the values in our application.However we can overcome this by using the SPUtility class provided by SharePoint which is available in Microsoft.SharePoint.Utilities namespace also the SPUtility class is available in sandbox solutions which provides speeder development.

To get the path of 12/14 hive folder we can use the below piece of code
Code:- SPUtility.GetGenericSetupPath(string.Empty);

Output:-C:\Program Files\Common Files\Microsoft Shared\web server extensions\14
If yous pass "templates" as a parameter to the method it will return the output as

Code:- SPUtility.GetGenericSetupPath("TEMPLATE");

Output:-C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\

So the way this class works is it will just concatenate the path till 14 hive to the value specified in the parameter for example if we pass "YYY" as a parameter to the method the output will be

Code:- SPUtility.GetGenericSetupPath("YYY");

Output:-C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\YYY\

The class does not check for the existence of the "YYY" folder under 14 hive it just returns the concatenation value.

2. Concatenation of Urls
We can use SPUtility.ConcatUrls method to concat the two strings and to return a url.The ConcatUrls method takes two parameter and returns a final url.For example
string firstUrl = "http://test";
string secondUrl = "Pages/test76.aspx";
string finalurl = SPUtility.ConcatUrls(firstUrl, secondUrl);
The value of finalurl would be http://test/Pages/test76.aspx i.e we need not to care about the slashes "/"  with the ConcatUrls method.Sharepoint takes care of it internally

3. Redirecting to OOB SharePoint error page
The SPUtility class provides TransferToErrorPage method to redirect a user to error page provided by SharePoint.The TransferToErrorPage method provides overload to provide link url and link text.

In simplest way to test the functionality of TransferToErrorPage  method we can use the below code

Exception ex = new Exception("Some error message");
SPUtility.TransferToErrorPage(ex.Message);

3. Redirecting to OOB Sharepoint success page
The SPUtility class provides TransferToSuccessPage method to redirect a user to success page provided by SharePoint.The TransferToSuccessPage method provides overload to provide next url, link url and link text.

In simplest way to test the functionality of TransferToSuccessPage method we can use the below code

SPUtility.TransferToSuccessPage("Operation was completed", @"default.aspx", "", "");

4.Sending Email from SharePoint 
The SPUtility class provides the SendEmail method to send out the mails from SharePoint.It utilizes the email configuration settings done in central administrator.This is a very useful method as we need not to maintain any configuration in our code as it directly uses the configuration done in central admin for sending out the mails.
You can call SPUtility.SendEmail method with appropriate parameters to send out the mail.Its return type is boolean which gives a better exception management in sending out the mail.we can pass the appropriate headers in send mail method by using a StringDictionary class.


No comments:

Post a Comment