隔板人生

 

使用程序方式控制sharepoint网站模版

public void ProvisionWebSettings(SPWeb web) {
   PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);
   ProvisionWebSettings(pweb);
  }

  public void ProvisionWebSettings(PublishingWeb pweb) {

   string settingsID = null;

   // Find this web in the webs hierarchy
   MatchCollection matches = Regex.Matches(pweb.Web.ServerRelativeUrl.ToLower(), @"/([^/]+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

   string xpath = "//webs";
   foreach (Match m in matches) {
    xpath += string.Format("/web[@url='{0}']", m.Result("$1"));
   }

   XmlNode xweb = ProvisionXml.SelectSingleNode(xpath);

   if (xweb != null)
    settingsID = GetAttributeValue(xweb, "settingsID");

   if (string.IsNullOrEmpty(settingsID)) {
    XmlNode xstemplate = ProvisionXml.SelectSingleNode(string.Format("/provision/templates/template[@name='{0}#{1}']", pweb.Web.WebTemplate.ToLower(), pweb.Web.Configuration));
    settingsID = GetAttributeValue(xstemplate, "settingsID");
   }

   // Couldn't find the settingsID, so abort
   if (string.IsNullOrEmpty(settingsID)) return;


   XmlNode xsettings = ProvisionXml.SelectSingleNode(string.Format("//websettings/settings[@id='{0}']", settingsID));
   if (xsettings == null) return;


   // Setup all page layouts
   XmlNode xlayoutroot = xsettings.SelectSingleNode("pagelayouts");
   XmlNodeList xlayouts = xlayoutroot.SelectNodes("pagelayout");

   bool inheritAvailablePageLayouts = false;
   if (xlayoutroot != null)
    inheritAvailablePageLayouts = Convert.ToBoolean(GetAttributeValue(xlayoutroot, "inherit", "false"));

   if (!inheritAvailablePageLayouts && (xlayouts != null && xlayouts.Count != 0)) {

    PageLayout[] layouts = new PageLayout[xlayouts.Count];
    int i = 0;
    string filename;

    foreach (XmlNode xlayout in xlayouts) {
     filename = GetAttributeValue(xlayout, "filename");
     if (!string.IsNullOrEmpty(filename)) {
      layouts[i] = SPPublishingHelper.GetPageLayout(filename, pweb.Web.Site);
      i++;
     }
    }
    pweb.SetAvailablePageLayouts(layouts, false);
   }


   // Setup all web templates
   XmlNode xtemplateroot = xsettings.SelectSingleNode("templates");
   XmlNodeList xtemplates = xtemplateroot.SelectNodes("template");

   bool inheritAvailableWebTemplates = false;
   if (xtemplateroot != null)
    inheritAvailableWebTemplates = Convert.ToBoolean(GetAttributeValue(xtemplateroot, "inherit", "false"));

   if (!inheritAvailableWebTemplates && (xtemplates != null && xtemplates.Count != 0)) {
    Collection<SPWebTemplate> templates = new Collection<SPWebTemplate>();
    SPWebTemplateCollection vtemplates = pweb.Web.Site.GetWebTemplates(1033);
    string name;

    foreach (XmlNode xtemplate in xtemplates) {
     name = GetAttributeValue(xtemplate, "name");
     if (!string.IsNullOrEmpty(name)) {
      templates.Add(vtemplates[name]);
     }
    }

    pweb.SetAvailableWebTemplates(templates, 1033, false);

   }

   // Setup navigation attributes
   XmlNode xnav = xsettings.SelectSingleNode("navigation");

   bool inheritGlobalNavigation = true;
   bool inheritCurrentNavigation = true;
   
   bool includeInGlobalNavigation = true;
   bool includeInCurrentNavigation = true;
   bool includeSubSitesInNavigation = true;
   bool includePagesInNavigation = false;

   OrderingMethod ordermethod = OrderingMethod.Manual;
   AutomaticSortingMethod sortmethod = AutomaticSortingMethod.Title;
   bool sortAscending = true;

   if (xnav != null) {

    inheritGlobalNavigation = Convert.ToBoolean(GetAttributeValue(xnav, "inheritGlobalNavigation", "true"));
    inheritCurrentNavigation = Convert.ToBoolean(GetAttributeValue(xnav, "inheritCurrentNavigation", "true"));

    includeInGlobalNavigation = Convert.ToBoolean(GetAttributeValue(xnav, "includeInGlobalNavigation", "true"));
    includeInCurrentNavigation = Convert.ToBoolean(GetAttributeValue(xnav, "includeInCurrentNavigation", "true"));
    includeSubSitesInNavigation = Convert.ToBoolean(GetAttributeValue(xnav, "includeSubSitesInNavigation", "true"));
    includePagesInNavigation = Convert.ToBoolean(GetAttributeValue(xnav, "includePagesInNavigation", "false"));

    XmlNode xordering = xnav.SelectSingleNode("ordering");

    if (xordering != null) {
     ordermethod = (OrderingMethod)Enum.Parse(typeof(OrderingMethod), GetAttributeValue(xordering, "type", "Manual"));
     sortmethod = (AutomaticSortingMethod)Enum.Parse(typeof(AutomaticSortingMethod), GetAttributeValue(xordering, "sortMethod", "Title"));
     sortAscending = Convert.ToBoolean(GetAttributeValue(xordering, "sortAscending", "true"));
    }

   }

   pweb.NavigationOrderingMethod = ordermethod;
   pweb.NavigationAutomaticSortingMethod = sortmethod;
   pweb.NavigationSortAscending = sortAscending;

   pweb.IncludeInGlobalNavigation = includeInGlobalNavigation;
   pweb.IncludeInCurrentNavigation = includeInCurrentNavigation;
   pweb.IncludeSubSitesInNavigation = includeSubSitesInNavigation;
   pweb.IncludePagesInNavigation = includePagesInNavigation;

   if (!pweb.Web.IsRootWeb) {
    pweb.InheritGlobalNavigation = inheritGlobalNavigation;
    pweb.InheritCurrentNavigation = inheritCurrentNavigation;
    if (inheritAvailableWebTemplates) pweb.InheritAvailableWebTemplates();
    if (inheritAvailablePageLayouts) pweb.InheritAvailablePageLayouts();
   }

   pweb.Update();

  }

posted on 2007-11-21 17:35  隔板小兵  阅读(225)  评论(0)    收藏  举报

导航