Tuesday, July 3, 2012

Access the sharepoint master page gallery programatically


Today we will access the Sharepoint Masterpage Gallery and delete the existing sub-folder. 


So first create one window console application using visual studio 2010.


Then add the Microsoft.Sharepoint.dll reference from the ISAPI folder from 14 hive directory( "C:\Program 


Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI").


So below are my source code


namespace DeleteFolderFromMasterPageGallery
{
class Program
{


static void Main(string[] args)
{
Program obj = new Program();
obj.GetMasterPageGallery();
}


public void GetMasterPageGallery()
{
string siteURL = "http://server:port/";
using (SPSite site = new SPSite(siteURL))
{
SPWeb web = site.RootWeb;
SPList list = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
web.AllowUnsafeUpdates = true;
SPFolder fol;
try
{
fol = list.RootFolder.SubFolders["Target Folder"];//This subfolder we will going to delete
}
catch
{ fol = null; }
if (fol != null)
fol.Delete();
web.AllowUnsafeUpdates = false;
}
}
}
}


Now run the console application so it will delete the existing sub folder named "Target Folder".




Regards
Hiren Patel

No comments:

Post a Comment