Code Snippets

The code example below demostrate how to populate the all sites collection tree structure into a System.Windows.Forms.TreeView:

private void GetSitesCollection()
{
  SPSecurity.RunWithElevatedPrivileges(delegate
  {
   Uri serverUri = new Uri(http://localhost/);
   SPWebApplication webApp =  SPWebApplication.Lookup(serverUri);
   foreach (SPSite siteCollection in webApp.Sites)
   {
     treeView1.Nodes.Add(siteCollection.HostName);
      foreach (SPWeb web in siteCollection.AllWebs)
      {
        treeView1.Nodes[0].Nodes.Add(web.Url).ToolTipText =
        web.RootFolder.ServerRelativeUrl; 

      }
   }
   treeView1.ExpandAll();
  });
}

The code example below demonstrates how to programmatically attach a page layout to a Publishing page in MOSS 2007:

This ChangeLayout method will first query the Page library of a selected web, it will then do a Chekin/Checkout check as well as checking if the page is in a draft state. It will then call the GetPageLayout method and finally applies the selected page layout to the selected page. A MOSS page within the Page library that you want to change the page layout the actual page layout that you want to apply to the page.

private void ChangePageLayout(string page, string pageLayout)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        using (SPSite site = new SPSite("http://localhost"))
        {
            using (SPWeb web = site.AllWebs['/sitename'])
            {
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["Pages"];
                SPQuery qr = new SPQuery();
                qr.Query ="<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + name + "</Value></Eq></Where>";
                qr.RowLimit = 1;
               //publishing web from the currently open web
                PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
                PublishingPageCollection getPAge = pubWeb.GetPublishingPages(qr);
                if (getPAge.Count > 0)
                {
                    SPListItem item = getPAge[0].ListItem;
                    PublishingPage page = getPAge[0];
                    // The file may be checked out so we now need to check it in
                    if (item.File.Level == SPFileLevel.Checkout)
                    {
                        item.File.CheckIn("Check in by the SharePoint team programmatically", SPCheckinType.MajorCheckIn);
                        item = item.File.Item;
                        MessageBox.Show("File was check out.");
                    }
                    // The file isn't checked out but it is in a draft state so we need to publish it.
                    else if (item.Level == SPFileLevel.Draft  item.ModerationInformation == null)
                    {
                        item.File.Publish("Published programmatically");
                        item = item.File.Item;
                        MessageBox.Show("File was not check out. But it was in a draft state so it was published it.");
                    }
                    //The file is published so we can update the field
                    else
                    {
                        try
                        {
                            item.File.CheckOut();
                            page.Layout = GetPageLayout(web, pageLayout);
                            item.Update();
                            item.File.CheckIn("Check in by the SharePoint team programmatically", SPCheckinType.MajorCheckIn);
                            MessageBox.Show("Page updated");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("error:  "+ ex.Message);


                        }
                    }
                }
                web.AllowUnsafeUpdates = false;
            }
        }
    });
}


This method gets all of the available page layout of a particular SPWeb object and returns a selected page layout.

private PageLayout GetPageLayout(SPWeb site, string pageLayout)
{
    PageLayout pl = null;
    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(site);
    PageLayout[] layouts = pubWeb.GetAvailablePageLayouts();
    foreach (PageLayout availableLayout in layouts)
    {
        if (pLayout.Name.Equals(pageLayout))
        {
            pl = pLayout;
        }
    }
    return pl ;
}

No comments: