Pages

Search This Blog

Monday, April 4, 2011

Creating & Publishing SPFolder

In a scenario I have to create and publish a folder in the Pages Library Programaticaly. This post shows how we can Create and Publish a SPFolder in the Default Pages Library in a Publishing Web.

Code Snippet:
private void CreateFolder(SPWeb objWeb,string FolderName )
{
Guid PagesLibraryId = PublishingWeb.GetPagesListId(objWeb);
if (PagesLibraryId != null)
{
objList = objWeb.Lists[PagesLibraryId];
// Create New Folder
SPListItem newFolder = objList.Items.Add(objList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, FolderName);
newFolder.Update();
// Publishing a specific Folder
SPFolder objfolders = objList.RootFolder.SubFolders[FolderName];
bool isFolderExists = objWeb.GetFolder(objList.RootFolder.ServerRelativeUrl +
"/" +).Exists;
if (isFolderExists)
{
if (objFolder.Item.ModerationInformation.Status !=
SPModerationStatusType.Approved)
{
objFolder.Item.ModerationInformation.Status =
SPModerationStatusType.Approved;
objFolder.Item.Update();
}
}
//Publishing All Folders in the List
SPFolderCollection objfolders = objList.RootFolder.SubFolders;
foreach (SPFolder objFolder in objfolders)
{
if (objFolder.Item != null)
{
if (objFolder.Item.ModerationInformation.Status !=
SPModerationStatusType.Approved)
{
objFolder.Item.ModerationInformation.Status =
SPModerationStatusType.Approved;
objFolder.Item.Update();
}
}
}
}

No comments:

Post a Comment