Modifying web.config programatically (SharePoint2010)

Here i'm going to add 2 web.config entries through code first i'm taking the backup copy of the original web.config and i'm adding the new entries.
Code shown below...

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Web.Administration;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Reflection;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace ConsoleApplicationWebConfigBackUp
{
class Program
{
static void Main(string[] args)
{
ApplyWebConfigModifications("http://localhost:5050");
//Taking the backup of the web.config file
string physicalPath = oSite.WebApplication.IisSettings[SPUrlZone.Default].Path.ToString();
File.Copy(physicalPath + @"\web.config", physicalPath + @"\web.config_backup_" + DateTime.Now.ToString("dd_mm_yyyy_hh_mm_ss") + ".config");
Console.Read();
}
        private static SPWebService contentService = null;
private static SPWebApplication webApplication = null;

public static bool ApplyWebConfigModifications(string webAppUri)
{
try
{
webApplication.WebConfigModifications.Clear();
//Adding the RuntimeFilter element in the web.config
SPWebConfigModification spWebConfigModification = null;
webApplication.WebConfigModifications.Remove(spWebConfigModification);
spWebConfigModification = new SPWebConfigModification();
spWebConfigModification.Owner = "UserName";
spWebConfigModification.Path = "configuration/SharePoint";
spWebConfigModification.Name = "RuntimeFilter[@Assembly='AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2a73b4939c24f7ff']";
spWebConfigModification.Value = "<RuntimeFilter Assembly='
AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2a73b4939c24f7ff' Class='ClassName' BuilderURL='audience_chooser.aspx'/>";
spWebConfigModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; spWebConfigModification.Sequence = 0; webApplication.WebConfigModifications.Add(spWebConfigModification); webApplication.Update(); contentService.ApplyWebConfigModifications();
                //anonymousIdentification
spWebConfigModification = new SPWebConfigModification();
spWebConfigModification.Owner = "UserName";
spWebConfigModification.Path = "configuration/system.web";
spWebConfigModification.Name = "anonymousIdentification";
spWebConfigModification.Value = "<anonymousIdentification enabled='true'/>";
spWebConfigModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
spWebConfigModification.Sequence = 0;
webApplication.WebConfigModifications.Add(spWebConfigModification);
webApplication.Update();
contentService.ApplyWebConfigModifications();
webApplication.Update();
contentService.ApplyWebConfigModifications();

}
catch
{
throw;
}
}
}
}

Comments

Popular posts from this blog

javascript to get the all site user in sharepoint 2010 (Getting all Site users using Client Object Model)

Adding Navigation Links To Quick Launch Bar Programatically (SharePoint 2010)

Disable all day event, Recurrence,Workspace Fields in Calendar list