Pages

Subscribe:

Thursday, January 26, 2012

SPDisposeCheck for SharePoint Development

Download location: http://code.msdn.microsoft.com/spdisposecheck


SPDisposeCheck is a tool that helps developers and administrators check custom SharePoint solutions that use the SharePoint Object Model helping measure against known Microsoft dispose best practices. This tool may not show all memory leaks in your code and may produce false positives which need further review by subject matter experts.

The SPDisposeCheck updated tool remains a standalone command line utility and have been added to a Visual Studio 2008/2010 IDE Add-In which calls out to the SPDisposeCheck.  This Add-In was originally presented at the SPC 2009 and is now available publically.  In addition this version has been tested with both WSS 3.0 + MOSS 2007 and SharePoint 2010 (Foundation and Server) environments.

Finally, it is encouraged you to run the updated SPDisposeCheck tool on all customized SharePoint projects to help identify areas in code which may lead to memory pressure and server stability issues.  As a best practice you should consider adding this tool to your SharePoint software development life cycle build process and review its output with a subject matter expert on a regular interval.

SharePoint 2007 and WSS 3.0 Dispose Patterns With Example

Wednesday, January 18, 2012

Programmatically Create a Site from Custom Site Template in SharePoint 2010


Here is the code snippet to create a Site using a Custom Site Template in SharePoint 2010.
     
using (SPSite site = new SPSite("http://sp2010:1234/"))
      {
                    using (SPWeb newWeb = site.OpenWeb())
                    {                         
                        newWeb.AllowUnsafeUpdates = true;                      
                        SPWebCollection subsites = newWeb.Webs;
                        SPWeb newSubWeb = subsites.Add("Site url", " Site name ", "Site description", 1033, {Name of the Site Template}, true, false);
                        newWeb.Update();
                    }
                }


 for eg: {Name of the Site Template} --> "{9D26F676-B943-451B-BED4-A8657A9C0FDF}#1InternalProject"


To get the custom Site Template GUID and Site Template Name you can use the below code in SharePoint 2010 even if the Template is in Solution Gallery. 
SPWebTemplateCollection templates = newWeb.GetAvailableWebTemplates(1033, true);
                        foreach (SPWebTemplate template in templates)
                        {
                           lblTemplate.Text  +=  template.Name;
                        }

Tuesday, January 10, 2012

TryGetList method in SharePoint 2010

Generally when we writing the code to get a list from the web, we will get  the error if the list does not exist. For that we will loop through the lists collection and checks whether it exists or not

           SPSite Site = SPContext.Current.Site;
            SPWeb Web = Site.OpenWeb();
            SPList  List = Web.Lists["List Name"];


If there is no list with the name ListName, it throughs null reference exception. To avoid this, In 2010 OM a new method is introduced in the SPListCollection .Simply use this method instead of looping all the lists and check its existance

            SPList List = Web.Lists.TryGetList("List Name");

            if (List != null)
              {
                 Response.Write("List Exist");
              }
           else 
                 Response.Write("List Doesnot Exist");

Happy Coding..!!!!

The List is too large to save as a template

The List is too large to save as a template. The size of a template cannot exceed 10485760 bytes.



If you try to save a list as a list template, you may see the following error due to the list being too large: The list is too large to save as a template. The size of a template cannot exceed 10485760 bytes.
You can increase the maximum size of a list / site template by running the following command (note that 500mb is the largest you can go):

stsadm -o setproperty -propertyname max-template-document-size -propertyvalue 50000000