Sharing Data Between Web Front Ends With Thread Safe Web Properties

Remember back in the old days when you only had one frontend? Remember when you thought that was complex?

Times have changed, and now in almost every production environment you have several frontends and, of course, that’s good – they boost performance. With SharePoint it’s simple to add more frontends if you feel you are falling short, but consequently this has also taken other tasks to a new level of complexity.

Software, like AppFabric, will allow you to persist data and share it between all your servers, which it works well, but do you have time to install and configure frontends everywhere just to share a 10 character string?

Luckily, you don’t have to. You can use SharePoint Web Properties to do so. Frontends are fast to write, to read, to code and you don’t need to configure anything.

SharePoint Web Properties are a bit tricky to use though (only a bit), particularly when you are using them in a multithreaded process, but hopefully these wrappers save you some time. (NB. These have been used in a couple scenarios and they work as expected, but we cannot assure that they are completely safe.)

1.  static object PropertyWriteLock = new object();  
2.    
3.  /// <summary>  
4.  /// Sets a web property to a given value. This method is thread safe.  
5.  /// </summary>  
6.  /// <param name="Key">The Key for the web property (case insensitive)</param>  
7.  /// <param name="Value">Value to set the property to</param>  
8.  public static void SetWebProperty(this SPWeb Web, string Key, string Value)  
9.  {  
10.     Key = Key.ToLower();  
11.     lock (PropertyWriteLock) //It's better to have a lock just for the key we are working with. I'll post the trick maybe tomorrow.  
12.     {  
13.         if (GetWebPropertyThreadSafe(Web, Key) != Value)  
14.         {  
15.             Web.Properties[Key] = Value;  
16.   
17.             Web.Properties.Update();  
18.         }  
19.     }  
20. }  
21.   
22. /// <summary>  
23. /// Returns the web property with the given key. This method is thread safe.  
24. /// </summary>  
25. /// <param name="Key">The Key for the web property (case insensitive)</param>  
26. /// <returns>Returns null if not found</returns>  
27. public static string GetWebPropertyThreadSafe(this SPWeb Web, string Key)  
28. {  
29.     Key = Key.ToLower();  
30.     using (SPSite site = new SPSite(Web.Site.ID))  
31.     {  
32.         using (SPWeb newWeb = site.OpenWeb(Web.ID))  
33.         {  
34.             return newWeb.GetWebProperty(Key);  
35.         }  
36.     }  
37. }  
38.   
39. /// <summary>  
40. /// Returns the web property with the given key.  
41. /// </summary>  
42. /// <param name="Key">The Key for the web property (case insensitive)</param>  
43. /// <returns>Returns null if not found</returns>  
44. public static string GetWebProperty(this SPWeb Web, string Key)  
45. {  
46.     Key = Key.ToLower();  
47.   
48.     return Web.Properties[Key];  
49. }  
50.   
51. /// <summary>  
52. /// Removes the web property from the web  
53. /// </summary>  
54. /// <param name="Key">The Key for the web property (case insensitive)</param>  
55. /// <remarks>The web property will remain there but set to null.</remarks>  
56. public static void RemoveWebProperty(this SPWeb Web, string Key)  
57. {  
58.     if (Web.Properties.ContainsKey(Key))  
59.         Web.Properties.Remove(Key);  
60.   
61.     Web.Properties.Update();  
62.   
63.     if (Web.AllProperties.ContainsKey(Key))  
64.         Web.AllProperties.Remove(Key);  
65.   
66.     Web.Update();  
67. }  

SharePoint Frontends have simplified parts of the application significantly, to reiterate – they are fast. Give them a go and let us know your thoughts.

This post was originally published on www.codegrimoire.com