Search This Blog

Monday, August 20, 2012

How to Change the Master Page for All Sites in Sharepoint 2010 Using Console Application

 Change the Master Page for All Sites in Sharepoint 2010 Using Console Application:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Collections;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace UpdateMasterPages
{
    class Program
    {
          static void Main(string[] args)
        {
            using (SPSite currentSite = new SPSite("http://server/sites/"))
            {
                SPWebCollection webCollection = currentSite.AllWebs;
                foreach (SPWeb web in webCollection)
                {
                    string masterPageUrl = string.Format("{0}/_catalogs/masterpage/custom.master", 
                   currentSite.ServerRelativeUrl).Replace("//", "/");
                    web.CustomMasterUrl = masterPageUrl;
                    web.MasterUrl = masterPageUrl;
                    web.Update();
                    //web.AllowUnsafeUpdates = true;
                    Trace(web.Title + "********" + web.Url);
                }
            }
        }
            public static void Trace(string stritrace)
            {
            Stream codeFile = File.Open("c:\\Logs.txt", FileMode.Append);
            StreamWriter Tex = new StreamWriter(codeFile); ;
            Tex.WriteLine(DateTime.Now.ToString() + " " + stritrace);
            Tex.Write(Tex.NewLine);
            Tex.Close();
            }
    }
}

1 comment:

  1. These are genuinely enormous ideas in concerning blogging.

    You have touched some pleasant factors here.
    Any way keep up wrinting.

    Review my page :: income tax cash advance

    ReplyDelete