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.

26 comments:

  1. INTERNATIONAL CONCEPT OF WORK FROM HOME
    Work from home theory is fast gaining popularity because of the freedom and flexibility that comes with it. Since one is not bound by fixed working hours, they can schedule their work at the time when they feel most productive and convenient to them. Women & Men benefit a lot from this concept of work since they can balance their home and work perfectly. People mostly find that in this situation, their productivity is higher and stress levels lower. Those who like isolation and a tranquil work environment also tend to prefer this way of working. Today, with the kind of communication networks available, millions of people worldwide are considering this option.

    Women & Men who want to be independent but cannot afford to leave their responsibilities at home aside will benefit a lot from this concept of work. It makes it easier to maintain a healthy balance between home and work. The family doesn't get neglected and you can get your work done too. You can thus effectively juggle home responsibilities with your career. Working from home is definitely a viable option but it also needs a lot of hard work and discipline. You have to make a time schedule for yourself and stick to it. There will be a time frame of course for any job you take up and you have to fulfill that project within that time frame.

    There are many things that can be done working from home. A few of them is listed below that will give you a general idea about the benefits of this concept.

    Baby-sitting
    This is the most common and highly preferred job that Women & Men like doing. Since in today's competitive world both the parents have to work they need a secure place to leave behind their children who will take care of them and parents can also relax without being worried all the time. In this job you don't require any degree or qualifications. You only have to know how to take care of children. Parents are happy to pay handsome salary and you can also earn a lot without putting too much of an effort.

    Nursery
    For those who have a garden or an open space at your disposal and are also interested in gardening can go for this method of earning money. If given proper time and efforts nursery business can flourish very well and you will earn handsomely. But just as all jobs establishing it will be a bit difficult but the end results are outstanding.

    Freelance
    Freelance can be in different wings. Either you can be a freelance reporter or a freelance photographer. You can also do designing or be in the advertising field doing project on your own. Being independent and working independently will depend on your field of work and the availability of its worth in the market. If you like doing jewellery designing you can do that at home totally independently. You can also work on freelancing as a marketing executive working from home. Wanna know more, email us on workfromhome.otr@gmail.com and we will send you information on how you can actually work as a marketing freelancer.


    Internet related work
    This is a very vast field and here sky is the limit. All you need is a computer and Internet facility. Whatever field you are into work at home is perfect match in the software field. You can match your time according to your convenience and complete whatever projects you get. To learn more about how to work from home, contact us today on workfromhome.otr@gmail.comand our team will get you started on some excellent work from home projects.


    Diet food
    Since now a days Women & Men are more conscious of the food that they eat hence they prefer to have homemade low cal food and if you can start supplying low cal food to various offices then it will be a very good source of income and not too much of efforts. You can hire a few ladies who will help you out and this can be a good business.

    Thus think over this concept and go ahead.

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

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

    ReplyDelete
  4. Your article is extremely helpful and have great information. I value your endeavors and all the best. It's exceptionally helpful data. I will sit tight for your next post. Keep It up.
    HP Helpline Number

    ReplyDelete

  5. 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
  6. 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
  7. 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
  8. I am feeling so cheerful to examine your blog post,really dazzling post you have shared, thanks


    Acer Customer Service

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

    Lenovo Contact Number

    ReplyDelete
  10. 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
  11. 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
  12. 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

  13. 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

  14. 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
  15. 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

  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 >>>
    SEO agency in delhi

    SEO companies in delhi


    unmanageable psychiatric patients in delhi


    unmanageable psychiatric patients

    ReplyDelete

  17. 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
  18. 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
  19. 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
  20. Very nice blog, you given useful information, Very great article, Thank you for sharing this wonderful article.
    amazon cloud services in india

    ReplyDelete




  21. 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