Search This Blog

Monday, August 20, 2012

How to Change the Master Page for Single Subsite in Sharepoint 2010 Using Console Application


Change the Master Page for Single Subsite 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 System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace UpdateMasterPages
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite currentSite = new SPSite("http://server/subsite/"))
            {
               SPWebCollection webCollection = currentSite.AllWebs;
               SPWeb web = currentSite.OpenWeb();
                    try
                    {
                        string masterPageUrl = string.Format("{0}/_catalogs/masterpage/custom.master", 
                        currentSite.ServerRelativeUrl).Replace("//", "/");
                        web.CustomMasterUrl = masterPageUrl;
                        web.MasterUrl = masterPageUrl;
                        web.Update();
                        Trace(web.Title + "********" + web.Url);
                    }
                    finally
                    {
                        if (web != null)
                            web.Dispose();
                    }
            }
        }
        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();
        }
    }
}


No comments:

Post a Comment