About Me

My photo
JHC Technology is a Service Disabled, Veteran-Owned, Small Business based in the Washington, DC Metro area. Our primary focus is to offer customized solutions and IT consulting to our Commercial and Government clients. Our experts have a broad experience delivering and managing Microsoft Enterprise applications and Cloud and Virtualization Solutions, as well as mobilizing Enterprise data.

Friday, May 31, 2013

How to Add and Remove Entries From Web.Config Using SPWebConfigModification

Often times during SharePoint Development, we need to store configuration related entries in the web.config file. We can use Property Bag tool from CodePlex, but in case you would like to use a Feature, I have outlined below the steps to create a Feature that will add entries to the web.config for the SharePoint Web Application:

1.       Open Visual Studio 2010
2.       Select New Project  Empty SharePoint Project
3.       Provide a local site and Select "Deploy as a farm solution" option.


4.       Right Click on the "Features" and Select "Add Feature"


5.       On the following screen, provide the Unique Title Name and Description. (Refer to screen below).

(Mostly, developers don't put anything in Description but this information really comes in handy when you are trying to figure out what a specific Feature is supposed to do in terms of functionality)
6.       Right Click on Feature1.Feature and Click on "Add Event Receiver"
7.       Uncomment the "Feature Activated" part of the code


8.       Add the following Code in FeatureActivated Method

This code is added to the following entries in web.config
  1. Connection Strings
  2. Key Value pair in AppSettings section
For each child node entry provide a specific owner name (like "Smartrider" in my case).We will need this as a reference later on in case you want to remove the entries (we are going to do this during Deactivation)
string value = @"Test"
SPWebApplication webApp = SPWebApplication.Lookup(new
Uri("<URL>"));
SPWebService service = SPWebService.ContentService;
SPWebConfigModification connMod = new
SPWebConfigModification();
connMod.Path = "configuration";
connMod.Name = "connectionStrings";
connMod.Value = " <connectionStrings> </connectionStrings>";
connMod.Owner = Assembly.GetExecutingAssembly().FullName;
connMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection;
webApp.WebConfigModifications.Clear();
webApp.WebConfigModifications.Add(connMod);
//SQL Connetion String
SPWebConfigModification webConfigMod = new
SPWebConfigModification();
webConfigMod.Owner = "Smartrider";
webConfigMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
webConfigMod.Value = String.Format("<add name=\"{0}\" connectionString=\"{1}\" providerName=\"{2}\"></add>", "conn", "Data Source=<datasource>;Initial Catalog=<databasename>;Integrated Security=True", "System.Data.SQLClient");
webConfigMod.Path = "configuration/connectionStrings";
webConfigMod.Name = "sqlConn";
webApp.WebConfigModifications.Add(webConfigMod);
//Add NetworkShare URL
SPWebConfigModification appConfigMod = new
SPWebConfigModification();
appConfigMod.Owner = "Smartrider"
appConfigMod.Path = "configuration/appSettings";
appConfigMod.Name = String.Format("add [@key='NetworkURL'] [@value='{0}']", value);
appConfigMod.Value = String.Format("<add key='NetworkURL' value='{0}' />", value);
appConfigMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
webApp.WebConfigModifications.Add(appConfigMod);
webApp.Update();
service.ApplyWebConfigModifications();
  • You also want to add code to remove the entry when the Feature is Deactivated. One way to do this is to find all the entries that are inserted by Owner name "Smartrider".
public
override
void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = SPWebApplication.Lookup(new
Uri("URL"));
try
{
RemoveEntries(webApp);
webApp.Update();
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.WebConfigModifications.Clear();
}
catch (Exception ex)
{
throw ex;
}
}
//Remove Entries made to web.config by Owner name "Smartrider"
private
void RemoveEntries(SPWebApplication webApp)
{
try
{
List<SPWebConfigModification> entriesToRemove = new
List<SPWebConfigModification>();
foreach(SPWebConfigModification configMod in webApp.WebConfigModifications)
{
if(configMod.Owner == "Smartrider")
{
entriesToRemove.Add(configMod);
}
}
if(entriesToRemove.Count > 0)
{
for (int i = entriesToRemove.Count - 1; i >= 0; i--)
{
webApp.WebConfigModifications.Remove(entriesToRemove[i]);
}
}
}
catch
{
throw;
}
}
9.     Deploy the code and check the web.config file to verify your entries.
10.   Done

Hemant Datta is the COO  for JHC Technology.  He can be reached at hdatta(at)jhctechnology.com, @hdatta, or connect with him on LinkedIn.

36 comments:

  1. Thanks for sharing the useful code,very informative for me.Please keep updating.
    Epson Support Number

    ReplyDelete
  2. I really need that kind of code,your website is awesome i love it.Thanks for disclosing with us.
    Dell customer service

    ReplyDelete

  3. Thanks for sharing this awesome piece of info. It is really very helpful.
    Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective to the topic which i was researching for a long time.
    Lexmark Helpline Number

    ReplyDelete
  4. Hey! Nice post thanks for sharing this.....if you want any solution related to windows than visit here:
    Windows Support Number


    http://www.hardwareforums.com/threads/pictures-corrupted-after-cropping-with-windows-10-photo-app.53673/

    ReplyDelete
  5. Great post! Thanks you so much for the share. Keep up the Excellent work,
    we look forward to reading more from you in the future!

    Mobile Apps |Mobile Development

    ReplyDelete
  6. I am feeling so cheerful to examine your blog post,really dazzling post you have shared, thanks


    Acer Customer Service

    ReplyDelete
  7. Hii.. it's a very wonderful blog with many ideas.. keep updating me..

    Lenovo Contact Number

    ReplyDelete
  8. This is my first time to visit here. I found countless interesting
    stuff in your weblog, especially in its discussion. I guess I’m not the only one having
    all the entertainment here! Keep up the outstanding work.


    Epson Printer Support

    Epson Technical Support

    Epson Helpline

    Epson Customer Service

    HP Customer Service

    HP Contact Number

    HP Support Number

    HP Helpline Number

    Canon Support

    Canon Support Number

    Canon Customer Service

    Canon Customer Care

    ReplyDelete
  9. I am feeling so cheerful to examine your blog post,really dazzling post you have shared and I got so many information from your blog,Thanks
    Lenovo Contact Number

    ReplyDelete
  10. Hi I am so excited I found your blog, I really
    found you by accident, while I was searching on Bing for something else, Anyhow
    I am here now and would just like to say many thanks for a remarkable post and a all
    round interesting blog (I also love the theme/design), I
    don’t have time to read through it all at the minute but
    I will be back to read a lot more, Please do keep up the awesome work
    Kindle Customer Service
    Lenovo Support

    ReplyDelete

  11. I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day
    If you are crazy for new fashion then try this >>>

    visit here


    visit here


    visit here


    visit here


    visit here


    visit here


    visit here


    visit here


    visit here

    visit here

    ReplyDelete

  12. Hello there, just became aware of your site via Google, and discovered that it’s truly useful. I’ll be happy if you continue this in future.

    Hotmail Support Number UK
    Lenovo Contact Number UK

    ReplyDelete
  13. This is my first time to visit here. I found countless interesting stuff in your weblog, especially in its discussion. I guess I’m not the only one having all the entertainment here! Keep up the outstanding work. If you are looking for Digital Marketing Services, then visit here >>>
    SEO agency in delhi

    SEO companies in delhi


    unmanageable psychiatric patients in delhi


    unmanageable psychiatric patients

    ReplyDelete

  14. This is my first time to visit here. I found countless interesting stuff in your weblog, especially in its discussion. I guess I’m not the only one having all the entertainment here! Keep up the outstanding work. If you are looking for Digital Marketing Services, then visit here >>>


    unmanageable psychiatric patients in delhi


    unmanageable psychiatric patients

    ReplyDelete
  15. It's an uncommonly nice post. Genuinely refreshing to examine it till the end thanks divide such a wonderful post..

    affordable banquet in delhi

    best banquet halls in rajouri garden

    magnified healing

    magnified healing classes

    ReplyDelete
  16. This is my first time to visit here. I found countless interesting stuff in your weblog, especially in its discussion. I guess I’m

    not the only one having all the entertainment here! Keep up the outstanding work. If you are looking for Digital Marketing Services,

    then visit here >>>
    best website development company in delhi

    best website designing company in delhi

    inpatient facility for mental patients

    de-addiction centre delhi

    virtual classroom service providers in india

    podium manufacturers in delhi

    best jewellery stores in delhi

    best jewellery showroom in south delhi

    best interior designer in delhi

    interior designer in delhi

    ReplyDelete
  17. Very nice blog, you given useful information, Very great article, Thank you for sharing this wonderful article.
    amazon cloud services in india

    ReplyDelete




  18. Such a wonderful blog.. I found so many useful stuff from your blog.. Keep updating us like this.. If you are looking for any service regarding immigration go here >>>

    Immigrate to Canada

    Apply Canada Permanent Resident Visa

    ReplyDelete


  19. This is one more mind blowing site page that has demonstrated me stunning resources which cements instructional activities and materials to the enormous class of point subject.


    Canada Immigration Visa

    Canada Immigration Program

    Canada Permanent Resident Visa

    ReplyDelete



  20. Hello! I just wish to give an enormous thumbs up for the nice info you've got right here on this post. I will probably be coming back to your weblog for more soon


    Google Digital Unlocked final Exam Answers| Free Free Free

    Google Web Designers Basics Free

    Check Instagram Profile Pictures in Full Size

    ReplyDelete
  21. I must say a nice article has been written by the author.
    Covering this topic in a single article was very difficult but I see that how nicely the author has done it.
    Author has given very amazing facts and information on this interesting topic.


    Immigration Consultancy Services
    Top Immigration Consultants
    CRS Calculator
    Express Entry Points Calculator

    ReplyDelete

  22. Really, this article is to a great degree one of the most flawlessly awesome ever. I am an antique 'Article' gatherer and I now and again read some new articles if I find them entrancing. Besides, I found this one extremely enchanting and it should go into my aggregation. Great work!

    digital marketing services in delhi
    website designing company delhi

    ReplyDelete

  23. I must say a nice article has been written by the author.
    Covering this topic in a single article was very difficult but I see that how nicely the author has done it.
    Author has given very amazing facts and information on this interesting topic.

    immigration office in nehru place delhi
    canada visa consultants in nehru place
    Canada PR Visa Consultants
    PR Visa experts

    ReplyDelete

  24. Wonderfull information given by you guys!! I hope that i'll be back again here !! Thanks

    visa immigration consultants in delhi

    canada visa experts in delhi

    Easy Immigration

    ReplyDelete
  25. Wow... such a nice topic you are discussing here and what a info you are giving us.. thanks a lot dear.. keep updating us like this.Digital Marketing Service

    ReplyDelete
  26. While researching for apartment friendly screen doors, i digressed and came across your blog. Thank you so much for sharing this information, it's really helpful. So i am still researching for window screens for my house. Do you guys have some kind of solution for me. I am looking for something similar to the ones in the link below.

    https://www.freedomscreens.net/freedom-infinity-zipline.php

    would love to know if you have anything regarding this. Please let me know if you do. :)

    ReplyDelete
  27. Informative blog! keep your family from mosquitoes/insects Mosquito net doors and windows

    ReplyDelete