Saturday, September 25, 2010

Add system.serviceModel to web.config

Share/Save/Bookmark

Here is the code built on the Sharepoint API that will add WCF/web service configuration, system.serviceModel to web.config.


            SPWebConfigModification item = new SPWebConfigModification();
            item.Name = "system.serviceModel";
            item.Value = " <>... ";
            item.Path = "configuration";
            item.Sequence = 0;
            item.Owner = "owner";
            item.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            try
            {
                SPWebApplication parent = properties.Feature.Parent as SPWebApplication;
                if (parent != null)
                {
                    parent.WebConfigModifications.Clear();
                    // To know why i have used Clear(), read this
                    parent.Farm.Services.GetValue().WebConfigModifications.Clear();
                    parent.WebConfigModifications.Add(item);
                    parent.WebService.ApplyWebConfigModifications();
                    parent.Update();

                }
            }

Same code can be used to add any entires to web.config by giving respective path.
To know other approaches of modifying web.config read Ways to Modify web.config

  Subscribe

No comments:

Post a Comment