Pages

Subscribe:

Wednesday, December 14, 2011

Disposing SharePoint Objects

If you are using SharePoint object model, then it is very much important to correctly release resources. It should ensure that any unmannaged resources should be released as soon as they are no longer needed.Though garbage collector automatically releases memory allocated to unused objects, but it will release ina unpredictable manner. The .NET Framework provides the IDisposable interface, which exposes a Dispose method that you should call to explicitly release these unmanaged resources. To invoke Dispose method, you can use the using keyword, you can use the try/finaly code block or you can explicitely invoke Dispose method.
Example of using keyword:
using (SPSite site = new SPSite("URL of the Site Collection"))
{
// Code here
}

Internally the compiler converts the using block to try/finally block as shown below.
SPSite site = null;
try
{
site = new SPSite("URL of the Site Collection");
// Code here
}
finally
{
if (site != null)
site.Dispose();
}

The SPSite and SPWeb types both implement the IDisposable interface, and both allocate unmanaged memory. If you do not correctly release SPSite and SPWeb instances, you will probably experience memory leaks, crashes, and frequent application pool recycles.

But if they are derived from the context object then the framework will take care of releasing resources, you should not release the resources.
For example SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb().
Here the web object is derived from the SPContext, so we should not dispose that. Here SPWeb object web.Dispose() automatically called.

Create SharePoint list programmatically C#

In this article I am going to show how to create a SharePoint List and fields Programmatically using c#
public void CreateList()
{
    //choose your site
    SPSite site = new SPSite("http://sps2010");
    SPWeb web = site.OpenWeb();
    SPListCollection lists = web.Lists;

    //create new Generic list called "Custom List"
    lists.Add("Custom List ", "list Description",SPListTemplateType.GenericList);
    SPList newList = web.Lists["Custom List "];

    //create Text type new column called "Name"
    newList.Fields.Add("Name", SPFieldType.Text, true);

/*create lookup type new column called "Lookup Column"
    Here I am going to get the information from the "Title"
    column of a list called "LookUp List"
   * /
    SPList lookupList = web.Lists["LookUp List"];
    newList.Fields.AddLookup("Lookup Column", lookupList.ID, false);
    SPFieldLookup lkp = (SPFieldLookup)newList.Fields["Lookup Column"];
    lkp.LookupField = targetList.Fields["Title"].InternalName;
    lkp.Update();

    //make new column visible in default view
    SPView view = list.DefaultView;
    view.ViewFields.Add("Name");
    view.ViewFields.Add("Lookup Column ");
    view.Update();
}

Thursday, December 1, 2011

Custom Error Page

With 2007, you can cancel events and return an error message to the user, but that provides limited interactivity  and not much help to the user beyond what your error message says. With 2010 events, you can cancel the event on your synchronous events and redirect the user to a custom error page that you create. The custom error pages and redirection will only work for pre-synchronous events,so you cannot do this for post-synchronous events such as ListAdded.


The way to implement custom error pages is to set the status property on your property bag for your event receiver to SPEventReceiverStatus.CancelWithRedirectedUrl, set the RedirectUrl property to a relative URL for your error page, and set the cancel property to true.


      properties.Cancel = true;
      properties.Status = SPEventReceiverStatus.CancelWithRedirectedUrl;
      properties.RedirectUrl = "/_layouts/mycustomerror.aspx";

Create a User Profile Using the Object Model


public void CreateUserProfile()
{
//We have to get the Context of the service application
strUrl = "http://site";
SPSite spSite = new SPSite(strUrl);
        ServerContext siteContext = ServerContext.GetContext(spSite);
        
       //UserProfileManager class contains member for profiles
       UserProfileManager upManager = new UserProfileManager(siteContext);       
       string strUserName = "domain\\user";
       if (!upManager.UserExists(strUserName ))
       upManager.CreateUserProfile(strUserName);
}