How to save the configuration information is a problem that must be encountered when developing a sharepiont application. Here we list 10 ways in which they have different application environments and their advantages and disadvantages.
1. Use the config file of APP/Web
SharePoint-related web. config packages are contained in the following folders in the file system:
- /Inetpub/wwwroot/WSS/virtualdirectories/port number-defines the Web. config file for the SharePoint content web application.
- /Inetpub/wwwroot/WSS/virtualdirectories/Management Center Port Number-defines the Web. config file for the SharePoint Management Center application.
- /Inetpub/wwwroot/WSS/virtualdirectories/port \ wpresources-the Web. config file used in the Web Part resources of the Web application.
- // Program/files \ common files \ microsoft shared \ Web Server Extensions \ wpresources-the web. config file used in the Web Part resources cached by the global assembly.
- % ProgramFiles % \ common files \ microsoft shared \ Web Server Extensions \ 14 \ config-define the web. config file and Other. config files used to extend the configuration settings of other web applications.
- % ProgramFiles % \ common files \ microsoft shared \ Web Server Extensions \ 14 \ ISAPI-defines the web. config file for the/_ vti_bin virtual directory.
- % ProgramFiles % \ common files \ microsoft shared \ Web Server Extensions \ 14 \ template \ layouts-defines the web. config file for the/_ layouts virtual directory.
When creating a Sharepoint Foundation web application. the config file is located in the content root folder of the Web application, which defines configuration settings for the server (for example, HTTP processing for Web parts ).
Another web. config file used for Web Part resources is located in the \ wpresources folder in the same root folder.
You can program to modify some of the preceding webconfig files (such as spwebconfigmodification class using Microsoft. Sharepoint. Administration namespaces ).
The Web. config method is applicable to scenarios where the configuration information needs to be effective within the scope of the Web-application, but the application is a little heavy.
2. Use Sharepoint list
Some people use creating data tables in the database to access configuration information. This is not a good method, but it is not a good method in SharePoint solution development. You can use a Sharepoint list instead of a database table. Because it is relatively simple, the created Sharepoint list can contain two data columns: Title and value, in this way, you can save the configuration information to this list. The advantage of using this method is that you do not need to develop another user management interface to manage your configuration information, because in Sharepoint, these are automatically available with the list, you only need to set the relevant permission on this list (because not everyone can view and modify it ). Of course, this is also insufficient, that is, you need additional configuration information to tell you where to look for such a list, it seems that it has come back again.
3. Use splist. rootfolder. Properties
If you want to access the configuration information associated with the list, you can use splist. rootfolder. properties.
// Get value
Splist. rootfolder. properties ["vti_listservertemplate"] = myvalue
// Set Value
String myvalue = splist. rootfolder. properties ["vti_listservertemplate"]
4. Use Web part Storage
We can use a format similar to [personalizable (personalizationscope. Shared), webpartstorage (storage. Shared)] Public String title {} to access configuration information related to Web parts.
# Region webpart Properties
[Webbrowsable (true ),
Category ("Facebook Settings "),
Personalizable (personalizationscope. Shared ),
Webpartstorage (storage. Shared ),
Defaultvalue (""),
Webdisplayname ("User ID/User Name "),
Webdescription ("Please enter user ID")]
Public String userid {Get; set ;}
[Webbrowsable (true ),
Category ("Facebook Settings "),
Personalizable (personalizationscope. Shared ),
Webpartstorage (storage. Shared ),
Defaultvalue ("10 "),
Webdisplayname ("wall count "),
Webdescription ("Please enter number of wall you want to display")]
Public int wallcount {Get; set ;}
# Endregion
5. Use spweb object
Spweb object has a persistent stringdictionary attribute named properties, which can be used to save specific configuration information related to the site, and this configuration can be effective throughout your farm. You can use the following code to operate on it:
Spweb = ...;
// Set the value
Web. properties ["thesetting"] = "the value ";
// Update the value
Web. properties. Update ();
// Get the value
String val = web. properties ["thesetting"];
Therefore, this method is very effective for saving configuration information in the key/value format, and it is not convenient to save complicated configuration information, you need to store the sequences of relatively complex configuration information into XML mode and serialize/deserialize (serialization/deseralization ). Of course, you need to obtain the manged web permission to perform these operations. In addition, you need to develop your own user operation interface to manage the configuration information.
6. Use spsite object
Unlike spweb object, spsite object does not have the same properties as spweb object properties. Therefore, if you want to access site collection-level configuration information, you can use spsite. rootweb is a special spweb object.
7. Use spwebapplication Properties
If you want to access configuration information at the web application level, but you do not want to modify web. config, you can use spwebapplication object. The spwebapplication object is inherited from the sppersisted object. It also has a hashtable attribute named properties. You can use it to access configuration information in the key/value format, complicated information can also be processed through serialization. The sample code is as follows:
Spwebapplication webapp =...
Object OBJ =...
// Set Value
Webapp. Add ("thesetting", OBJ );
// Persist it
Webapp. Update ();
This method requires you to perform operations with the farm administrator permission and you need to build your own operation management interface.
8. Use custom sppersistedobjects
Maybe custom sppersistedobjects is the best way to access the configuration information of Web applications. The advantage of this method is that it is more convenient to access complicated configuration information, you can create a class that inherits from sppersistedobject, set the configurations to be accessed to attributes of this class, and mark these attributes as persisted. The sample code is as follows:
Public class thesettings: sppersistedobject {public thesettings (){}
Public thesettings (string name, sppersistedobject parent, guid ID): Base (name, parent, ID ){}
[Persisted] site. rootweb. rootfolder. properties. Add ("sitename", "yoursite ");
Public String webserviceurl;
}
Then you can use the spwebapplication getchild method to extract the configuration information you saved above.
Thesetasksettigs = webapplication
. Getchild <thesettings> ("thesettings ");
You can create two static methods to facilitate your operations. Of course, this method still requires farm administrator and you need to create your own management interface.
Public static thesetaskgetsettings (spwebapplication webapplication) {thesetasksettings = webapplication. getchild <thesettings> ("thesettings"); If (settings = NULL) {return thesettings. createnew (webapplication);} return settings;} public static thesetaskcreatenew (spwebapplication webapplication) {return New thesettings ("thesettings", webapplication, guid. newguid ());}
9. Use the XML file in feature folder
You can save the configuration information to an XML file and place the file under the corresponding feature folder. This mode features that the configuration information does not depend on SharePoint, in addition, users are generally not allowed to directly modify (that is, static configuration information is stored ).
10. A config store framework
You can obtain this framework (SharePoint config store). This framework allows you to save configuration information to the list and provides APIs to extract your saved configuration information, these configuration information can be complex objects, which are saved in XML format.
Here we will list 10 ways to access configuration information in SharePoint solution development. The sample code is just a reference. You need to check the specific technical text to use the relevant content, the application code is not important because it only scans the access configuration information.
I believe you have used other methods. Please write them out and share them with you.
Reprinted: http://www.cnblogs.com/wsdj-ITtech/archive/2012/04/16/2440653.html