This class can be used from a console app, inside a feature , a web part or an aspx page.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Publishing;
using System.Collections.Specialized;
using System.Collections;
namespace Leelodharry.Sharepoint
{
public class Utils
{
///
/// Sets a webs welcome page.
///
///
///
public static bool SetWelcomePage(SPWeb web, string welcomepage)
{
try
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pagecollection = pubWeb.GetPublishingPages();
PublishingPage page = pagecollection[welcomepage];
pubWeb.DefaultPage = page.ListItem.File;
pubWeb.Update();
}
catch
{
return false;
}
return true;
}
///
/// Deletes a page given it's web and it's page i.e. pages/default.aspx
///
///
///
public static bool DeletePage(SPWeb web, string pagename)
{
try
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pagecollection = pubWeb.GetPublishingPages();
PublishingPage page = pagecollection[pagename];
page.ListItem.Delete();
}
catch
{
return false;
}
return true;
}
///
/// Create a page publish and approve it.
///
///
///
///
public static bool CreatePage(SPWeb web, string pagename, string pagelayout)
{
try
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
//find layout from list.
PageLayout[] layouts = pubWeb.GetAvailablePageLayouts();
PageLayout layout = null;
for (int idx = 0; idx < layouts.Length; idx++)
{
layout = layouts[idx];
if (layout.Name.ToUpper() == pagelayout.ToUpper())
{
break;
}
}
if (pagelayout == null)
{
return false;
}
PublishingPageCollection pagecollection = pubWeb.GetPublishingPages();
PublishingPage page = pagecollection.Add(pagename, layout);
//include in navigation can be set.
page.CheckIn("EIG Homepage");
SPListItem litem = page.ListItem;
litem.File.Approve("Approved");
}
catch
{
return false;
}
return true;
}
///
/// Cleans slashes from a URL.
///
///
///
public static string CleanURL(string url)
{
return url.Replace("//", "/");
}
///
/// Sets the web to allow all templates and all page layouts.
///
///
public static void SetAllPagesAndTemplate(SPWeb web)
{
PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);
pubweb.AllowAllPageLayouts(false);
pubweb.AllowAllWebTemplates(false);
pubweb.Update();
}
///
/// Sets the welcome page in a sharepoint web.
///
///
///
public static void SetDefaultPage(SPWeb web, string fileurl)
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
try
{
SPFile newDefaultPageFile = pubWeb.Web.GetFile(fileurl);
pubWeb.DefaultPage = newDefaultPageFile;
pubWeb.Update();
}
catch { }
}
///
/// Sets a sites logo
///
///
///
///
public static void SetSiteLogo(SPWeb web, string Url, string Description)
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
web.SiteLogoUrl = Url;
web.SiteLogoDescription = Description;
web.Update();
}
///
/// Changes a web's available page layouts.
///
///
///
///
public static void SetPageLayouts(SPWeb web, string commaseplist)
{
SPWeb RootWeb = web.Site.RootWeb;
SPSite site = web.Site;
PublishingSite pubSite = new PublishingSite(site);
PageLayoutCollection rootPageLayouts = pubSite.GetPageLayouts(true);
string siteurl = Utils.CleanURL(site.ServerRelativeUrl + "/_catalogs/masterpage/");
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
string[] pagelayoutnames = commaseplist.Split(',');
PageLayout[] pagelayouts = new PageLayout[pagelayoutnames.Length];
for (int idx = 0; idx < pagelayoutnames.Length; idx++)
{
string findPage = siteurl + pagelayoutnames[idx].Trim();
PageLayout layout = (PageLayout)rootPageLayouts[findPage];
pagelayouts[idx] = layout;
}
pubWeb.SetAvailablePageLayouts(pagelayouts, false);
pubWeb.Update();
}
///
/// Given a comma delimited list of available templates this procedure will then set the available sites in sharepoint.
///
///
///
///
public static void SetAvailableTemplates(SPWeb web, string commaseplist)
{
uint LCID = 1033;
SPWeb RootWeb = web.Site.RootWeb;
SPWebTemplateCollection sitetemplates = RootWeb.GetAvailableWebTemplates(LCID);
Hashtable HtSiteTemplates = new Hashtable();
for (int idx = 0; idx < sitetemplates.Count; idx++)
{
SPWebTemplate sitetmp = sitetemplates[idx];
HtSiteTemplates.Add(sitetmp.Name, sitetmp);
}
string[] SiteTemplateNames = commaseplist.Split(',');
System.Collections.ObjectModel.Collection
for (int idx = 0; idx < SiteTemplateNames.Length; idx++)
{
string findTemplate = SiteTemplateNames[idx];
SPWebTemplate template = (SPWebTemplate)HtSiteTemplates[findTemplate];
if (template != null)
{
useTemplates.Add(template);
}
}
web.SetAvailableWebTemplates(useTemplates, LCID);
web.Update();
}
///
/// Configures the "Parallel Approval" WorkFlow
///
///
public static void ConfigureWorkFlow(SPWeb web)
{
SPList pagesLib = web.Lists["Pages"];
SPWorkflowAssociation wf = pagesLib.WorkflowAssociations.GetAssociationByName("Parallel Approval", System.Globalization.CultureInfo.CurrentCulture);
if (wf != null)
{
wf.AssociationData = AssociationData.AssocData;
wf.Name = "Ecclesiastical Approval";
string assocData = wf.AssociationData;
//Update workflow.
pagesLib.UpdateWorkflowAssociation(wf);
}
}
///
/// Gets the nested level in the tree.
///
///
///
public static int GetWebDepth(SPWeb web)
{
int depth = 0;
while (!web.IsRootWeb)
{
web = web.ParentWeb;
depth++;
}
return depth;
}
///
/// Sets the master page for a web.
///
///
///
///
public static bool SetMasterPage(SPWeb web, string masterpage, string custommaster)
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
try
{
//pubWeb.MasterUrl = masterpage;
//pubWeb.CustomMasterUrl = custommaster;
//pubWeb.Update();
web.MasterUrl = masterpage;
web.CustomMasterUrl = custommaster;
web.Update();
pubWeb.Update();
}
catch
{
return false;
}
return true;
}
///
/// Sets the site's CSS.
///
///
///
public static bool SetCss(SPWeb web, string cssUrl)
{
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
try
{
//pubWeb.AlternateCssUrl = cssUrl;
web.AlternateCssUrl = cssUrl;
web.Update();
pubWeb.Update();
}
catch
{
return false;
}
return true;
}
public static bool CreateSubSite(SPWeb web, string sitedefinition, string title, string description, string url)
{
try
{
web.Webs.Add(url, title, description, 1033, sitedefinition, false, false);
web.Update();
}
catch
{
return false;
}
return true;
}
}
}
No comments:
Post a Comment