Monday, February 11, 2013

Disable All SharePoint Timer Jobs Console Application

I found I needed to disable all the timer jobs in SharePoint 2010. Since there are over 100 jobs, I wrote a quick console application to disable them all for me. Below is the code. You can alter line 34 to job.IsDisabled = false; to make this application enable all the SharePoint timer jobs instead.

Note: Be sure to change the Target Framework of the project to .NET Framework 3.5.
using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace DisableTimerJobs
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {         
                SPFarm farm = SPFarm.Local; 
                //Get all SharePoint Web services 
                SPWebService service = farm.Services.GetValue<SPWebService>("");

                int Count = 0;

                foreach (SPWebApplication webapp in service.WebApplications)
                {

                    foreach (SPJobDefinition job in webapp.JobDefinitions)
                    {

                        //this example is to run on one service only
                        //if (job.Name == "SchedulingNotification")
                        //{
                            Count++;

                            try
                            {
                                //disable all jobs
                                job.IsDisabled = true;
                                job.Update();                               

                                Console.WriteLine(Count + ") " + job.Name + ": ");

                                if (job.IsDisabled == true)
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                    Console.WriteLine("Disabled");
                                    Console.ResetColor();
                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine("Enabled");
                                    Console.ResetColor();
                                }                                
                               
                            }
                            catch (Exception ex)
                            {                                
                                Console.WriteLine(Count + ") " + job.Name + ": ");
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("ERROR: ");
                                Console.WriteLine(ex.ToString());
                                Console.ResetColor();
                                Console.ReadLine();
                            }                            
                            
                        //}
                    }                   
                    
                }
            }

            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR: ");
                Console.WriteLine("An error has occured: ");
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                Console.ReadLine();
            }

            Console.WriteLine("Done. Press enter to exit.");
            Console.ReadLine();
        }
    }
}

Tuesday, February 5, 2013

Add Metadata Tags in SharePoint 2010 to Improve SEO

SharePoint 2010 doesn’t have a built in mechanism to handle metadata tags for improved Search Engine Optimization (SEO). My solution for adding metadata tags uses a combination of out-of-the-box (OOB) functionality combined with code to allow dynamic addition of a meta title, meta description, and meta keywords on a per page basis for content editors. I tried to keep it very simple, easy, and extendable.  There are four major components to this solution:

1.       Set up the pages library
2.       Create a SEO user control
3.       Add the reference to the web control on the master page
4.       Fill in the content

Pages Library Set Up

Enterprise Keywords
Enable Enterprise Keywords – In the pages library, go to Library Settings > Enterprise Metadata and Keyword Settings. Check the top box Add an Enterprise Keywords column to this list and enable Keyword synchronization. This adds a column called Enterprise Keywords to the list which is tied to the Metadata Term Store.

Out-of-the-Box Columns
The title and comments field are also used for this solution, but they are already in the list for you. The comments column on the pages library is the same as the description for the page. I don’t know why Microsoft called it comments. This confuses many content editors. But they did and there’s nothing I can do about that...

SEO User Control

Create a new user control. I called my control SEOPageMetaTags.ascx. In addition to the standard namespaces at the top of the SharePoint control, you also need to add the web control namespace at the top.

<%@ Register TagPrefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" 
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" %>

Then add this code to SEOPageMetaTags.ascx:
<asp:PlaceHolder ID="MetaDescriptionHolder" runat="server">
<asp:Literal ID="metadescstart" runat="server"></asp:Literal><SharePointWebControls:FieldValue id="PageDescription" FieldName="Comments" runat="server"/><asp:Literal ID="metadescend" runat="server"></asp:Literal>
</asp:PlaceHolder>
<asp:PlaceHolder ID="MetaKeywordsHolder" runat="server">
<asp:Literal ID="metakeystart" runat="server"></asp:Literal><SharePointWebControls:FieldValue id="PageKeywords" FieldName="23f27201-bee3-471e-b2e7-b64fd8b7ca38" runat="server"/><asp:Literal ID="metakeyend" runat="server"></asp:Literal>
</asp:PlaceHolder>

It’s important to note that the metadata tags cannot be dynamically generated on the page, like with JavaScript, as this isn’t always able to be seen by crawlers. So, on the code behind (SEOPageMetaTags.ascx.cs), add the following code:

/// 
/// This control writes out the meta description and meta keywords in the page head used for SEO.
/// 
    public partial class SEOPageMetaTags  : UserControl
    {
        protected void  Page_Load(object sender, EventArgs e)
     {
        this.metadescstart.Text = "<meta  name=\"description\" content=\"";
        this.metadescend.Text = "\"  />";
        this.metakeystart.Text = "<meta  name=\"keywords\" content=\"";
        this.metakeyend.Text = "\"  />";
        }
     }

Ensure the SEOPageMetaTags.ascx page is uploaded/deployed to the location:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES

Master Page

Add this line at the top of the page so the master page knows what control to reference:

<%@ Register TagPrefix="MyCustomStuff" 
TagName="SEOPageMetaTags" 
Src="~/_controltemplates/SEOPageMetaTags.ascx" %>

Add this code in the <head> under the title tag:
<!-- for SEO --> 
<asp:ContentPlaceHolder id="SEOMetaTags" runat="server">
<MyCustomStuff:SEOPageMetaTags runat="server" />
</asp:ContentPlaceHolder>

Edit Page Properties


From the content editor perspective, the last step is to fill in the information on the page properties. Just edit the properties of the page and fill in the Title, Comments (same as page description), and the Enterprise Keywords field.

Benefits

There are three main benefits to using the Enterprise Keywords field in this situation.
1.       Keywords help a site’s SEO (albeit very minimally, it still helps).
2.       Keywords are standardized since they come from the Term Store.
3.       Enterprise Keywords show as refiners on your search results page so this enhances your page’s findability.
You could also use the terms entered here as a means to organize your content on your site, but that’s outside the scope of this article. It just goes to show that using Enterprise Keywords can do a lot of work for you that goes beyond just SEO and can help a ton especially if you have many content editors.
Finally, this solution is easy to extend. The SEO tags covered in this article included the Title tag, meta description, and meta keywords. You could add other tags as well. For example, an index tag to include the option for content editors to choose not to have a particular page indexed or to add a page canonical url. Using the separate web control for SEO allows you to quickly add whatever additional metadata you need.