Friday, August 21, 2009

Useful code for modifying a Site

I am currently working on a project that needs to support the clients SharePoint implementation. They do everything directly in production, so I have created a site definition to reproduce there site structure. One feature I created to help me with this is basically a web scoped feature that modifies the site with custom changes I want in code (I am a programmer after all!!).

The following code does some interesting stuff:
  • Creates a document library in code
  • Sets the document library title, enables versioning and allows content types in the document library
  • Finds a site content type and associates it with the document library
  • Changes the content type order so it contains just my content type
  • Sets the default page of the site to the default view of the document library
  • Adds some of my content type fields to the default view


public class ProjectSiteReceiver : SPFeatureReceiver
{
    private string _documentLibraryTitle = "Project Documents";
 
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = (SPWeb)properties.Feature.Parent)
        {
            SPListTemplateType templateType = SPListTemplateType.DocumentLibrary;
            Guid listId = web.Lists.Add(_documentLibraryTitle, null, templateType);
 
            SPList list = web.Lists[listId];
            list.Title = _documentLibraryTitle;
            list.EnableVersioning = true;
            list.ContentTypesEnabled = true;
            list.Update();          
 
            SPContentType contentType = web.GetContentType("Project Document");
            SPContentType listContentType = list.ContentTypes.Add(contentType);
            listContentType.Update();
 
            List<SPContentType> contentTypeList = new List<SPContentType>();
            contentTypeList.Add(listContentType);
            list.RootFolder.UniqueContentTypeOrder = contentTypeList;
            list.RootFolder.Update();
 
            SPFolder webFolder = web.RootFolder;
            webFolder.WelcomePage = list.DefaultView.Url;
            webFolder.Update();
 
            SPView view = list.DefaultView;
            view.ViewFields.Add("Project ID");
            view.ViewFields.Add("Project Type");
            view.Update();
        }
    }
 
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = (SPWeb)properties.Feature.Parent)
        {
            SPList list = web.GetListByName(_documentLibraryTitle);
            list.Delete();
        }
    }
 
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {
    }
 
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
    {
    }
}




PS: Sorry GetContentType and GetListByName are my own custom extension methods. There isn't much to them really, just better error handling then using the default SharePoint API methods with square brackets.

PPS: You also need to set the welcome page back to the default in the FeatureDeactivating method:

SPFolder webFolder = web.RootFolder;
webFolder.WelcomePage = "default.aspx";
webFolder.Update();

No comments: