Wednesday, April 28, 2010

Changing the Webpart properties programatically

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace webpartproperties
{
class Program
{
static void Main(string[] args)
{
SPSite spsSite = new SPSite("site");
SPWeb spwWeb = spsSite.OpenWeb();
spwWeb.AllowUnsafeUpdates = true;
SPWebPartCollection wpcWebParts = spwWeb.GetWebPartCollection("site", Storage.Shared);
foreach (WebPart wptWebPart in wpcWebParts)
{
//Console.WriteLine(wptWebPart);
if (wptWebPart.GetType().ToString() == "webpartname")
{
PropertyInfo[] pinProperties = wptWebPart.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo pinProperty in pinProperties)
{
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
if (pinProperty.Name == "your property name")
{

pinProperty.SetValue(wptWebPart, "select ", null);
wpcWebParts.SaveChanges(wptWebPart.StorageKey);
Console.WriteLine(pinProperty);
}
}
}
}
Console.ReadLine();

}
}
}

No comments:

Post a Comment