none
Want setting values like App.Config in Visual Studio 2010 sharePoint project

    Question

  • Hi,

    My issue is want to get value from config file (Like app.config we use for general .Net application) in my sharepoint project to set some Key and value which can access in code behind of my sharepoint project.

    Any thought will help me...

    Thanks in advance,

    ashiof

    Tuesday, May 08, 2012 8:57 AM

Answers

  • public static bool WebApplicationAppSettingsPresent()
     {
                using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
                {
                    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);
                    AppSettingsSection appSettings = config.AppSettings;
                    return appSettings.ElementInformation.IsPresent;
                }
    }

    Hi,

    The above code will allow you to read values from SharePoint's web.config

    I would recommend you use SharePoint property bag to store values rather than the web.config

    http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/05/12/how-to-write-a-value-into-the-property-bag.aspx

    Regards,

    Luke

    Tuesday, May 08, 2012 9:07 AM
  • Hi,

    You can make use of web.config file associated with each SharePoint web application, and use SPWebconfigModification class to read/write values from/to web.config file.

    Below is sample code to achive same:

    SPWebService service = SPWebService.ContentService;
     SPWebConfigModification myModification = new SPWebConfigModification();
     myModification.Path = "configuration/appSettings";
     myModification.Name = "add [@key='mySetting'] [@value='http://joelblogs.co.uk']";
    myModification.Sequence = 0;
     myModification.Owner = "ADJOELJ";
     myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
     myModification.Value = "<add key='mySetting' value='http://joelblogs.co.uk' />";
     /*Get reference to the WebApplication we want to administer*/ 
     SPWebApplication app = service.WebApplications["SharePoint - 80"];
     app.WebConfigModifications.Add(myModification);
      
     /*Call Update on the WebApplication to save changes*/ 
     app.Update();
     /*Call ApplyWebConfigModifications to cause the Content Service to update the web.config files*/ 
     service.ApplyWebConfigModifications();


    Thanks.... ________________ Baba (MCTS, MCPD)

    Tuesday, May 08, 2012 10:57 AM

All replies

  • public static bool WebApplicationAppSettingsPresent()
     {
                using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
                {
                    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);
                    AppSettingsSection appSettings = config.AppSettings;
                    return appSettings.ElementInformation.IsPresent;
                }
    }

    Hi,

    The above code will allow you to read values from SharePoint's web.config

    I would recommend you use SharePoint property bag to store values rather than the web.config

    http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/05/12/how-to-write-a-value-into-the-property-bag.aspx

    Regards,

    Luke

    Tuesday, May 08, 2012 9:07 AM
  • Hi,

    You can make use of web.config file associated with each SharePoint web application, and use SPWebconfigModification class to read/write values from/to web.config file.

    Below is sample code to achive same:

    SPWebService service = SPWebService.ContentService;
     SPWebConfigModification myModification = new SPWebConfigModification();
     myModification.Path = "configuration/appSettings";
     myModification.Name = "add [@key='mySetting'] [@value='http://joelblogs.co.uk']";
    myModification.Sequence = 0;
     myModification.Owner = "ADJOELJ";
     myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
     myModification.Value = "<add key='mySetting' value='http://joelblogs.co.uk' />";
     /*Get reference to the WebApplication we want to administer*/ 
     SPWebApplication app = service.WebApplications["SharePoint - 80"];
     app.WebConfigModifications.Add(myModification);
      
     /*Call Update on the WebApplication to save changes*/ 
     app.Update();
     /*Call ApplyWebConfigModifications to cause the Content Service to update the web.config files*/ 
     service.ApplyWebConfigModifications();


    Thanks.... ________________ Baba (MCTS, MCPD)

    Tuesday, May 08, 2012 10:57 AM