Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Locating the Target Web Part
To dynamically change the "Link" property of the Page Viewer Web part, you'll need two important pieces of information. The first is the URL of the site on which the Page Viewer Web part resides and the second is the URL value to which you wish to set the "Link" property.

Enable the use of reflection in your application by including it with a using statement. Use the SPSite object to obtain a reference to the site on which the Page Viewer Web part, residing in the form of a SPWeb object. The SPSite class represents a root site collection on a virtual server, i.e. the top-level site and all its sub-sites. Obtain a reference to the root site collection with the SPSite object:

using System.Reflection; SPSite spsSite = new SPSite("http://server/sites/web/default.aspx");

Once you've obtained the SPSite object reference, use it to obtain a reference to the SPWeb object using the OpenWeb() method. If your code is running from a command context, as in the example code, the next statement is not required. However, if your code is running in a Web part context—as a tool on a Windows SharePoint Services site, say, then you have to set the AllowUnsafeUpdates property to true in order for your changes to register.

SPWeb spwWeb = spsSite.OpenWeb(); spwWeb.AllowUnsafeUpdates = true;

Once you have obtained the reference to the SPWeb object, you'll use SPWeb's GetWebPartCollection() method to to obtain an instance of the SPWebPartCollection object (which contains all the Web parts on the site):

SPWebPartCollection wpcWebParts = spwWeb.GetWebPartCollection("http://server/sites/web/default.aspx", Storage.Shared);

GetWebPartCollection()'s first parameter specifies the URL of the current Web you're working on. Its second parameter specifies the view you'll have of the site from which you're retrieving the Web part list. Finally, the Storage.Shared value returns the list of Web parts which are visible to all users.

Before you can modify your collection of Web parts, you need to identify your Page Viewer Web part. A simple foreach loop iterates the collection, returning a reference to any Web parts in the collection. Simply check each Web part to find the relevant one.

Use reflection along with the GetType() method to check the type of Web part from within the loop. Using Intellisense in Visual Studio, explore the Microsoft.SharePoint.WebPartPages namespace to locate the class names of each of the standard Web parts in SharePoint. This is how you determine that the class name for the Page Viewer Web part is:


This article's example uses a direct compare for this value in order to avoid addressing the class name topic until this part of the code. However, in real world projects, you would probably want to define the value as a constant in your code.

The currently referenced Web part in your iteration has the power to reflect on itself. The System.Reflection reference provides it with the GetType() method to return the object type. Casting the returned value, which the ToString()method returns in System.Type format to a string, you can make a direct comparison to the target Web part type string which uses the full namespace convention. Once you find your target Page Viewer Web part, you can change it.

foreach (WebPart wptWebPart in wpcWebParts) { if (wptWebPart.GetType().ToString() == “Microsoft.SharePoint.WebPartPages.PageViewerWebPart”) { ... } }

If you've got more than one Page Viewer Web part on the target page, you'll need to employ additional property values in order to locate the exact right Page Viewer Web part you wish to modify. The Part Order could be used to help identify your target more clearly.

Comment and Contribute






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



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