Saturday, July 18, 2009

Enumerate Site collection and Site Content Types

. Saturday, July 18, 2009

I will be creating a web part that enumerates root site and all it's subsites content types along with their columns.

We will be using a Tree View control for this.

using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

/// <summary>
        /// Shows content types recursively
        /// </summary>
        /// <param name="oWeb">The parent node</param>
        /// <param name="oNode">The content Type node</param>
        protected void ShowCTypes(SPWeb oWeb, TreeNode oNode, SPContentTypeId contTypeID)
        {
            TreeNode oSubNode = null;
            SPContentTypeCollection oCTypeColl = oWeb.ContentTypes;

            foreach (SPContentType conttype in oCTypeColl)
            {
                //if current type is child of parameter oNode and is not the root ctype
                if (conttype.Parent.Id == contTypeID && conttype.Parent.Id != conttype.Id)
                {
                    oSubNode = new TreeNode(conttype.Name);
                    oSubNode.NavigateUrl = oWeb.ServerRelativeUrl + "/_layouts/ManageContentType.aspx?cType=" + conttype.Id;
                    oSubNode.Target = "_blank";

                    //add columns
                    ShowCtypeColumns(oSubNode, conttype);
                    //add subnodes
                    oNode.ChildNodes.Add(oSubNode);

                    //now oSubNode is the parent node
                    ShowCTypes(oWeb, oSubNode, conttype.Id);
                }
            }

        }

        /// <summary>
        /// Shows columns for a content type
        /// </summary>
        /// <param name="oNode">Current content type Treenode</param>
        /// <param name="contType">Content type whose columns are to be added</param>
        protected void ShowCtypeColumns(TreeNode oNode, SPContentType contType)
        {
            TreeNode oSubNode = null;

            //fetch fields of content type
            SPFieldCollection oFldColl = contType.Fields;

            if (oFldColl.Count != 0)
                oNode.ChildNodes.Add(new TreeNode("Columns"));

            foreach (SPField oField in oFldColl)
            {
                if (!oField.Title.Equals("ContentType") && !oField.Hidden)
                {
                    oSubNode = new TreeNode(oField.Title);
                    oSubNode.NavigateUrl = contType.ParentWeb.ServerRelativeUrl + "/_layouts/ManageContentTypeField.aspx?cType=" + contType.Id + "&Field=" + oField.InternalName + "&Fid=" + oField.Id;
                    oSubNode.Target = "_blank";
                    oNode.ChildNodes[0].ChildNodes.Add(oSubNode);
                }
            }

        }

/// <summary>
        /// Creates Treeview nodes for content types and features.
        /// </summary>
        protected void CreateControls()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                TreeView1.Nodes.Clear();
                TreeView1.Nodes.Add(new TreeNode("Content Types"));

                //get the name of the current web in current user's context

                string spSiteURL = SPControl.GetContextSite(Context).Url.ToString();

                //Initialize the spweb object using the elevated privileges
                using (SPSite oSite = new SPSite(spSiteURL))
                {
                    SPWebCollection oWebColl = oSite.AllWebs;

                    #region Add content types

                    TreeNode oSiteNode = null;
                    foreach (SPWeb oWeb in oWebColl)
                    {
                        //create website nodes
                        oSiteNode = new TreeNode(oWeb.Title);
                        oSiteNode.NavigateUrl = oWeb.Url;

                        //get contenttypes collection for each subsite including root site
                        SPContentTypeCollection oCTypeColl = oWeb.ContentTypes;
                        TreeNode oNode = new TreeNode();

                        //if site doesn't have any Ctypes (only content types defined for that particular site are displayed)
                        if (oCTypeColl.Count != 0)
                        {
                            oNode.Text = oCTypeColl[0].Name;
                            oNode.NavigateUrl = oWeb.ServerRelativeUrl + "/_layouts/ManageContentType.aspx?cType=" + oCTypeColl[0].Id.ToString();
                            oNode.Target = "_blank"; //opens link in new window

                            //send current site and root contenttype node (not the actual content type)
                            ShowCTypes(oWeb, oNode, oCTypeColl[0].Id);

                            //add root contenttype node to website node
                            oSiteNode.ChildNodes.Add(oNode);

                        }
                        //add website node to root node of treeview.
                        TreeView1.Nodes[0].ChildNodes.Add(oSiteNode);
                        oWeb.Dispose();
                    }
                    #endregion

 

TreeView1.CollapseAll();
                    oSite.Dispose();
                }
            } //end of delegate
            ); //end of RunWithElevatedPrivileges

        }

        protected override void OnPreRender(EventArgs e)
        {
            CreateControls();
        }

        protected override void CreateChildControls()        {
            //add control to treeview
            this.Controls.Add(TreeView1);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            //render treeview control
            TreeView1.RenderControl(writer);
        }

    }
}

0 comments: