Showing posts with label webpart. Show all posts
Showing posts with label webpart. Show all posts

Saturday, July 18, 2009

Enumerate Site collection and Site Content Types

. Saturday, July 18, 2009
0 comments

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);
        }

    }
}

Wednesday, June 3, 2009

SharePoint content page throw error or take forever to open

. Wednesday, June 3, 2009
0 comments

This might happen due to a faulty webpart like Content editor web part containing invalid content URL link.

SOLUTION: remove content URL from content editor webpart using SharePoint designer.

Error while closing/deleting webparts through SharePoint GUI

.
0 comments

Use SharePoint designer to close/delete them.

Change Stylesheet of a web part

.
0 comments

When we add a style sheet to webpart through code, it overrides the main stylesheet of the page. However there’s a workaround approach through we can restrict styles to only one webpart.

Every webpart has a Zone Cell Id reference “MSOZoneCell_WebPart_WPQ_”. The _WPQ_ portion is replaced by the webpartzone name during the execution of the code. This can be checked by viewing the source of the webpartpage. So it would become something like #MSOZoneCell_WebPartWPQ3.

Through view source property we can find the id of the webpart to which we want to add our styles.

Suppose our webpartpage has two webparts with a table each and we want to apply styles only to the second webpart. In usual cases the stylesheet, if supplied in the webpart code, is applied to the whole page, which is not required.

The solution:

1. What we can do is, to add a Content Editor Webpart, and supply the style definitions for that particular webpart in its source editor. This webpart can be hidden.

Now again if we supply a style something like:

<style>

TABLE

{

            font-weight: normal;

            color: navy;

            font-family: 'Bookman Old Style';

            background-color: #ccccff;

}

</style>

It would be applied to the whole page. The solution is that we can use the id of this particular webpartzone (MSOZoneCell_WebPartWPQ3).(it is fixed and can be obtained through view source property if the webpart page.

Hence we can have something like:

<style>

/*changes the header of the wbepart*/

#MSOZoneCell_WebPartWPQ3 .ms-WPTitle

{

    font-weight: bold;

    font-family: verdana, arial, helvetica, sans-serif;

    color: #00ccff;

    padding-left: 6px;

    padding-right: 7px;

    padding-top: 2px;

    padding-bottom: 2px;

    font-size: 8pt;

background-color: teal;

}

/*changes the body of this webpart only, if it contains any td tag.*/

#MSOZoneCell_WebPartWPQ3 .ms-WPBody TD

{

            border-right: thin groove;

            padding-right: 1px;

            border-top: thin groove;

            padding-left: 1px;

            padding-bottom: 1px;

            margin: 2px;

            border-left: thin groove;

            padding-top: 1px;

            border-bottom: thin groove;

}

#MSOZoneCell_WebPartWPQ3 .ms-WPBody th

{

    font-size: 8pt;

    font-family: verdana, arial, helvetica, sans-serif;

    background-color: #cc0033

}

/*since datagrid is rendered as a table , hence we can provide a custom class here for datagrid. This clas can be set as the cssclass of the datagrid in the webpart code.*/

.skynetdatagrid {

font-size: 12pt;

            color: silver;

            font-family: verdana, arial, helvetica, sans-serif;

            background-color: black;

}

.skynettable

{

            font-weight: bold;

            color: green;

            background-color: #ffccff;

}

</style>