Thursday, December 13, 2012

Basic of SharePoint


Some Basics of sharepoint which everyone should be aware of

Never create a sharepoint object with  SPContext.Current.Site

using (SPSite site = SPContext.Current.Site)
            {
            }
Well whats wrong with the above declaration?
If we see it closely we are creating a site obect in Using block which will dispose the sharepoint object for a current request hence it would cause errors in the next instances of spsite object
 
Always create a sharepoint object with below
 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
            }

Root Site Url

The below line of code will always return the url of root site
string rootSiteUrl = SPContext.Current.Site.Url;
 

Current web Url 

The below line of code will always return the url of current web
string currentWebUrl = SPContext.Current.Web.Url;
 

Objects used in Feature Receiver

The object returned by the SPFeatureReceiverProperties.Feature.Parent property for a site- or web-scoped feature receiver should not be disposed of: 
 

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    // Do stuff
}

Objects owned by Sharepoint 

Some objects are owned by SharePoint and must not be disposed of in custom code. The SPWeb object returned by SPSite.RootWeb is such an example.
This property returns a shared SPWeb instance and is used internally in several methods and properties. It should never be disposed of explicitly. The following sample code does not dispose of anything:

SPSite site = SPContext.Current.Site;
SPWeb rootWeb = site.RootWeb;
SPWeb web = SPContext.Web;
SPWeb web = SPContext.Current.Web;
SPSite site = SPContext.Site;
SPSite site = SPContext.Current.Site;                                        

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.