Wednesday, February 4, 2009

How to get the Webpart XML programmatically in moss

Hi All,

Today we are going to discuss about the getting XML of the web part programmatically in moss.

Whenever we want to place webpart as a feature on any page as part of deployment, we need to place the webpart XML under AllUserWebPart tag.

So here is a way to get the XML of any webpart from webpart gallery.

Go to top level site settings, under galleries go to web parts, select your webpart of which you want to view XML. Let’s say we want to see AdvanceSearchBox webpart’s XML.

Once you click there, you’ll be redirected to a page. Click on ViewXML button, that will take you to the XML that we want actually get.

Ok, this is way how we can get the XML of the webpart from using Sharepoint.

Now the same functionality we want to achieve through the coding and this is what the article is all about.

Here what we do is we first get a reference of webpart galleries because all webparts used in entire site will be there in the web part galleries.

First we will write down the SPQuery which will be fetching the actual webpart which is passed as parameter from the webpart galleries.

Then we will take up the GetCatelog method to return the webpart gallery which is again kind of a list.

Then we will simply fire query with the SPQuery’s object and get the SPListItemCollections and then using the BinaryStream we will take out the XML.

Ok , so without talking much let’s write down the code, you may write a function to have two parameters which are Web object and Webpart name and return XMLreader and then play with XMLReader. Here just for demo purpose I am writing a code in a webpart.

This code demonstrates looping through entire webpart collections and getting their XML representation.

System.Xml.XmlReader objXMLReader = null;

SPQuery objQuery = new SPQuery();

//Initialize Query to get all record from the webpart gallery, so
//query like where ID > 0

objQuery.Query = "<Where><Gt><FieldRef Name=\"ID\"/><Value Type=\"Counter\">0</Value></Gt></Where>";

//Consider your parent site accordingly,
//I am one level down to the parent site so I am referring like this
//if you are at top level site, ignore ParentWeb.

SPList objWebParts =objWeb.ParentWeb.GetCatalog(SPListTemplateType.WebPartCatalog);

SPListItemCollection objColl = objWebParts.GetItems(objQuery);

if (objColl.Count > 0)

System.Xml.XmlDocument objDoc = null;

foreach (SPListItem objItem in objColl)

Stream objStream = objItem.File.OpenBinaryStream();

StreamReader objReader = new StreamReader(objStream);

StringReader objStringReader = new StringReader(objReader.ReadToEnd());

objXMLReader = System.Xml.XmlReader.Create(objStringReader);

objDoc = new System.Xml.XmlDocument();




Now once you have the objDoc in your hand, just take out the InnerXML of that and there you go, you have your Webpart XML in your hand.

That’s it. Your job is done.

1 comment:

Anonymous said...

Thanks so much!!! Just what I needed!

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to