CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

SPWebConfigModification

SPWebConfigModification. Some links to start with:

http://panvega.wordpress.com/2009/09/02/using-spwebconfigmodification-within-a-feature-receiver/
http://www.onedotnetway.com/get-name-of-current-executing-assembly-in-c/
http://blogs.devhorizon.com/reza/?p=459
http://ikarstein.wordpress.com/2010/09/02/add-web-config-modification-with-powershell-spwebconfigmodification/
http://msdn.microsoft.com/en-us/library/bb861909.aspx

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
	var webapp = parent as SPWebApplication;
	if (webapp != null)
	{
		var mod1 = GetWebControlsConfigMod();
		webapp.WebConfigModifications.Add(mod1);
		var mod2 = GetConStringConfigMod();
		webapp.WebConfigModifications.Add(mod2);
		SaveChanges(webapp);
	}
	else
	{
		Log.Warning("no modifications to webapp are done");
	}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
	var webapp = parent as SPWebApplication;
	if (webapp != null)
	{
		var mod = GetWebControlsConfigMod();
		var modsCollection = webapp.WebConfigModifications;
		var modToDelete =
			(from m in modsCollection
			 where m.Value.Equals(mod.Value, StringComparison.InvariantCultureIgnoreCase)
			 select m).FirstOrDefault();
		if (modToDelete != null)
		{
			modsCollection.Remove(modToDelete);
			SaveChanges(webapp);
		}
	}
}
private static void SaveChanges(SPPersistedObject webapp)
{
	var service = webapp.Farm.Services.GetValue();
	service.ApplyWebConfigModifications();
	webapp.Update();
}

private static SPWebConfigModification GetWebControlsConfigMod()
{
	var assembly = System.Reflection.Assembly.GetAssembly(typeof(WebControls.OfficeEditor));
	var ass = assembly.FullName;
	var @namespace = assembly.GetName().Name + ".WebControls";
	var value = string.Format("", ass,
							  @namespace);
	const string path = "configuration/SharePoint/SafeControls";
	var mod = new SPWebConfigModification
	{
		Path = path,
		Name = "JustaName1",
		Sequence = 0,
		Owner = @namespace,
		Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
		Value = value
	};
	return mod;
}

private static SPWebConfigModification GetConStringConfigMod()
{
	const string name = "ContosoConString";
	const string conString =
		"Data Source=db.contoso.com;Initial Catalog=contoso_db;Persist Security Info=True;User ID=contoso_Contributor;Password=contoso";
	var mod = new SPWebConfigModification
	  {
		  Name = "JustaName2",
		  Path = "configuration/connectionStrings",
		  Owner = "contoso_connectionstrings",
		  Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
		  Value = String.Format("", name, conString)
	  };
	return mod;
}

Powershell:

#ADDING
asnp microsoft.sharepoint.powershell
$url = "http://contoso"
$app = Get-SPWebApplication $url
$name = "ContosoConString";
$conString = "Data Source=db.contoso.com;Initial Catalog=contoso_db;Persist Security Info=True;User ID=contoso_Contributor;Password=contoso";
$mod = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$mod.Name = "JustName2"
$mod.Path = "configuration/connectionStrings"
$mod.Owner = "User Name"
$mod.Type = 0 #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$mod.Value = '<add name="{0}" connectionString="{1}" />' -f $name, $conString
$app.WebConfigModifications.Add($mod)
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$service.ApplyWebConfigModifications()
$app.Update()
#ADDING snapin
asnp microsoft.sharepoint.powershell

$url = "http://contoso"
$app = Get-SPWebApplication $url

#SafeControl
$assembly = "Contoso.SP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=20c0327f8b01d979"
$namespace = "Contoso.SP.WebControls"
$mod = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$mod.Name = "ContosoSafeControls"
$mod.Path = "configuration/SharePoint/SafeControls"
$mod.Owner = "Contoso"
$mod.Type = 0 #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$mod.Value = '<SafeControl Assembly="{0}" Namespace="{1}" TypeName="*" Safe="True" />' -f $assembly, $namespace
$app.WebConfigModifications.Add($mod)

#applying changes
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$service.ApplyWebConfigModifications()
$app.Update()

#REMOVING
asnp microsoft.sharepoint.powershell
$app = Get-SPWebApplication http://contoso
$modCol = $app.WebConfigModifications
$mod = $modCol| Where { $_.Owner.Equals("User Name") }
$modCol.Remove($mod)
$service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$service.ApplyWebConfigModifications()
$app.Update()

#if you want to clear all web.config mods associated with the web app:
$modCol = $app.WebConfigModifications
while ($modCol -ne 0) { $modCol.Remove($modCol[0]) }
$service.ApplyWebConfigModifications()
$app.Update()

Allright, how can we apply changes in the whole farm? Ryan Dennis uses $app.Parent.Apply.. Rajkamal says we have to run service.Apply (if it would be in C#). To Get Service we can use generics in PowershellKeith Dahlby writes about SPWebService.ContentService.

Add the first connectionString

I encountered a problem adding the first connection string by this script. It just didn’t work. The solution is to add the parent element “connectionStrings”.

Advertisements

One response to “SPWebConfigModification

  1. Pingback: scriptcs and SharePoint. How SharePoint can benefit? | Bool Tech

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Coder, Speaker, Author

Office 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Paul J. Swider - RealActivity

RealActivity is a specialized healthcare services and solution advisory firm.

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

| Blockchain | Serverless Architecture | Microservices Architecture | DevOps | AWS Lambda | Teraform |

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: