Thursday 28 January 2010

Display a SharePoint list in quick launch - programatically

How to display a SharePoint list in quick launch programatically?

Below I have pasted the code I used to achieve this. This code is for a document library. But the methodology is same for a list as well (since a document library is also a list).

using (SPSite siteCollection = new SPSite(http://server:3000/sites/site1))
{
using (SPWeb webSite = siteCollection.OpenWeb())
{
try
{
Guid docLibGuid = webSite.Lists.Add("Document Library 1", "Document library", SPListTemplateType.DocumentLibrary);
SPList documentLibrary = webSite.Lists[docLibGuid];
documentLibrary.OnQuickLaunch = true;

documentLibrary.Update();
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.Message);
}
}
}


But I observed that if I use the code in green colour in this manner it doesn't work.

webSite.Lists[docLibGuid].OnQuickLaunch = true;
webSite.Lists[docLibGuid].Update();


It's strange....