Saturday, July 18, 2009

Enumerate SharePoint Features

. Saturday, July 18, 2009
0 comments

Enumerate Installed Features in the Farm

TreeView TreeView1 = new TreeView();

TreeView1.Nodes.Add(new TreeNode("Features"));
TreeNode oFSiteNode = null;
TreeNode oFNode = null;

SPSecurity.RunWithElevatedPrivileges(delegate()
{

//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))
                {


TreeView1.Nodes.Clear();

#region Return Installed Features.

                    oFSiteNode = new TreeNode("All Farm Installed Features");

                    SPFarm oFarm = oSite.WebApplication.Farm;

                    SPFeatureDefinitionCollection oFeatDefColl = oFarm.FeatureDefinitions;
                    foreach (SPFeatureDefinition oFeatDef in oFeatDefColl)
                    {
                        if (oFeatDef != null)
                        {
                            oFNode = new TreeNode(oFeatDef.GetTitle(new System.Globalization.CultureInfo(1033)));
                            oFSiteNode.ChildNodes.Add(oFNode);
                        }
                    }

                    TreeView1.Nodes[1].ChildNodes.Add(oFSiteNode);

#endregion

TreeView1.CollapseAll();
oSite.Dispose();

}

}

Enumerate Site collection and Site Content Types

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

    }
}

Tuesday, June 23, 2009

SharePoint ULS logs are empty: 0 KB logs in 12 Hive logs

. Tuesday, June 23, 2009
1 comments

A few months back I decided that it was time to get rid of unnecessary SharePoint logs since I wasn't into development and event logs were coming sufficient. Just in case you guys are wondering how to enable diagnostic logs, here are the steps: -

1) Open central administration > operations. Click on 'diagnostic logging' under 'Logging and Reporting'.

2) Select 'no' for 'Customer Experience Improvement Program' and 'ignore errors' for 'error reports'. If you wish to throttle (restrict) the events captured, select from category and events to be captured in event logs and trace logs (12 hive).

3) Provide the location for storing these logs. I usually keep 5-10 log files of 10-30 minutes each; otherwise my disk becomes full. As a practice keep logs in a different disk then the MOSS/WSS installation so that disk full issues do not affect the service.

NOTE: there are two types of logs in 12 Hive logs folder (<local-drive>\Program Files\Common Files\Microsoft Shared\web server extensions\12\Logs). The ones sitting outside the GUID folders are used for debugging and called ULS (unified logging service) logs. The ones within the GUID are usage analysis logs.

======================================================================

Anyway, back to my original issue; I happily disabled the logs, however few weeks back decided to re-enable them. And what, did everything as described above and the log files hence created were empty. Hmm, mind boggling; didn't look like a permission issue; still haven' figured out yet; the logs were being constructed but were of 0 KB each; files were empty: -

Things to check in his situtaion: -

  • enough disk space.
  • check if WSS timer service and WSS tracing service are running. Restart them once again.
  • try throttling the events (though this is most unlikely to work out as a solution since you would be restricting logs; exactly the opposite); however it worked for me; I enabled Verbose logging in trace log category of diagnostic logging page and logs started filling up. Whew!! Thank goodness, at least I have some data in logs now. This is a very strange behavior, what you guys should check is if you are throttling anything; remove that selection. For example throttling error events when no errors are there would give empty logs.

If anyone has figured out the solution, do let me know as my system is behaving exactly the opposite. :)

 

 

Monday, June 15, 2009

Error: The IIS Admin service terminated with the following error. The system cannot find the file specified

. Monday, June 15, 2009
1 comments

Today when I started my server (WIN 2k3), it threw a 'Service control manager' error: 'Failed to start at least one or more services'. I checked Event viewer and found the following error.

image

Upon manually trying to start IIS Admin from MMC , same error was thrown. I googled and as expected found an article - http://support.microsoft.com/kb/332103 from Microsoft saying "reinstall IIS". Err.. Definitely not. There has to be some other way. Soon I stumbled upon this angelic post - http://www.iisworkstation.com/2008/07/troubleshooting-could-not-start-iis.html. You can refer to the same post or in case are unable to open it, find below the instructions to troubleshoot this issue.

1) IIS ADMIN service needs two files - 'metabase.xml' and MBschema.xml to start; they reside in C:\WINDOWS\system32\inetsrv. The first thing to check is if files are available, in my case 'metabase.xml' was missing. I vaguely remembered that chkdsk that ran last night had mentioned some corrupt metabase.xml. Hmm.. so the poor file was removed... The first thing I did was to stop automatic startup of IIS ADMIN service from services.msc.

2) Fortunately, C:\WINDOWS\system32\inetsrv\History maintains the 10 latest backups (by default) of metabase.xml and MBScheme.xml files. As instructed, I copied the most recent entry (by modified date).

image

and pasted it to C:\WINDOWS\system32\inetsrv. Then I renamed it to 'metadata.xml'. Next I went to services.msc, changed the startup mode of IIS ADMIN from manual to automatic and started it. Immediately I got some script error. hmm. ...anyways, I clicked on yes and continued;the service started successfully.

3) Next thing to do was to increase the number of history files saved (learn from the past as I periodically backup SAM, SECURITY, SOFTWARE AND DEFAULT) . As instructed I ran the following command from start > run.

cscript.exe adsutil.vbs set /MaxHistoryFiles "15"

Ntime when the system restarts the effects will take place (else manually restart the IIS ADMIN service again).

4) Next thing was to go to 'INETMGR'. All the websites were in stopped state. I manually started them and checked in IE if my SharePoint sites were coming up fine. Finally YES YES YES.

Hope it helps anyone who faces this issue. Thanks to the original post!!