Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use the Power of Reflection to Dynamically Set SharePoint Web Part Properties : Page 4

While SharePoint's User Defined Site Template saves you work by allowing users to tweak their interface designs themselves, allowing them to save site templates creates work for you. If you want to tip the workload balance back in your favor, a little Reflection can go a long way.


advertisement
Making the Change
Before you can set your new values, you need to change the "Link" property of the Page Viewer Web part. To do this, you need to obtain the the "Link's" array of properties. Using the reflection method GetProperties() returns an array of PropertyInfo objects, each of which contain property information related to the type being reflected upon. GetProperties() takes a bitwise enumerated value for flags that control binding. Reflection conducts the search for members and types. The code below uses values that specify for all public, non-public, and instance members to be included in the reflection search.

PropertyInfo[] pinProperties = wptWebPart.GetType().GetProperties( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

After you obtain the array of properties for the Page Viewer Web part, you need to locate the target property. In the example of the Page Viewer Web part, the "Link" property in the user interface is stored in the "ContentLink" property in the property array. Using another foreach loop, iterate through the property array and check the property names until you locate the "ContentLink" property. Again, repeated usage of the property name would probably dictate it being stored in a constant in your code.

foreach (PropertyInfo pinProperty in pinProperties) { if (pinProperty.Name == “ContentLink”) { ... } }

To set the value, pass a reference to the Page Viewer Web part object—as well as to the new URL value to whose property you wish to set the SetValue() method. Commit these changes with a call to the Web part collection's SaveChanges() method.


pinProperty.SetValue(wptWebPart, “http://MyNewURLValueHere”, null); wpcWebParts.SaveChanges(wptWebPart.StorageKey);

Once this update has been made, terminate each foreach loop by issuing a break statement for each loop in turn.

using System.Reflection; SPSite spsSite = new SPSite(“http://server/sites/web/default.aspx”); SPWeb spwWeb = spsSite.OpenWeb(); spwWeb.AllowUnsafeUpdates = true; SPWebPartCollection wpcWebParts = spwWeb.GetWebPartCollection(“http://server/sites/web/default.aspx”, Storage.Shared); foreach (WebPart wptWebPart in wpcWebParts) { if (wptWebPart.GetType().ToString() == “Microsoft.SharePoint.WebPartPages.PageViewerWebPart”) { foreach (PropertyInfo pinProperty in pinProperties) { if (pinProperty.Name == “ContentLink”) { pinProperty[“ContentLink”].SetValue(wptWebPart, “http://MyNewURLValueHere”, null); wpcWebParts.SaveChanges(wptWebPart.StorageKey); break; } } break; } }

Reflection Allows for Flexibility
Reflection helps you dynamically update Web part properties so you can provide relevant site content. Using it, you can:
  1. Locate and identify your target Web part.
  2. Inspect its properties.
  3. Update those properties.
All this gives you the felixibility to dynamically respond to any changes in your SharePoint Technologies environments.

The accompanying download contains the project source code for a command line utility that modifies the Page Viewer Web part property on any specified site. Note that the project file has set default command line switches. You'll need to modify these if you intend to run the project in debug mode from Visual Studio. If you compile the application and run the .exe from the command line, standard parameter parsing rules apply.



Cornelius J. van Dyk is a Senior Technology Consultant with Crowe Chizek and Partners LLC. He lives in Indianapolis, IN and has been involved in SharePoint Technologies since the beta days of the 2003 version. A former Air Force pilot, he now specializes in SharePoint, BizTalk, and K2 technologies.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date