I followed this tutorial to make a tabzone webpart. if I want to manually add it to a page it works perfectly, but if I try to add it programmatically, it gives me out of index exception, which is because i need to set its XML property.

here's the real project link

Here is my Code for adding a webpart

using (SPSite mySite = new SPSite(PageUrl))
                using (SPWeb web = mySite.OpenWeb())

public void AddTabbedWebpart(SPWeb web)
            SPLimitedWebPartManager wpManger = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
            TabbedWebPartZoneTabs webpart = new TabbedWebPartZoneTabs();

            webpart.Title = "Survey Tabs";
            webpart.AllowClose = true;
            webpart.AllowMinimize = true;
            webpart.AllowConnect = true;
            webpart.ChromeType = PartChromeType.None;
            webpart.ChromeState = PartChromeState.Normal;
            webpart.AllowZoneChange = true;
            webpart.AllowHide = true;
            webpart.ExportMode = WebPartExportMode.All;
            webpart.Hidden = false;
            webpart.AllowEdit = true;
//webpart.ZoneTabXml = ??

            wpManger.AddWebPart(webpart, "Top", 1);

When i exported the webpart, i came to know that ZoneTabXML actually contains xml code something like this,



tab name="A"><

webPart title="2010 Last Day" 
visible="false" /&gt;&lt;webPart title="Dates" 
visible="false" /&gt;&lt;webPart title="Events" 
visible="false" /&gt;&lt;webPart title="asdasdasd" 
visible="false" /&gt;&lt;webPart title="asdasd asdasd" 
visible="false" /&gt;&lt;webPart title="asdasdasdas dd" 
visible="false" /&gt;&lt;webPart title="Ssdasdasd List" 
visible="false" /&gt;&lt;/tab&gt;&lt;tab name="Surdfgveys"&gt;&lt;

webPart title="Tasdsd asds" 
visible="false" /&gt;&lt;

webPart title="iiiiiiiiis" 
visible="false" /&gt;&lt;webPart title="ddddddfon" 
visible="false" /&gt;&lt;webPart title="Tdfdf fordfds" 
visible="false" /&gt;&lt;webPart title="Tdfdf dfes" 
visible="false" /&gt;&lt;webPart title="Tdfgerger es" 
visible="false" /&gt;&lt;/tab&gt;&lt;tab name="dfgfgdfd gs"&gt;&lt; carries on...

Now i got no clue how to setup this as property of webpart, as far as i know by the webpart is using HTML code to dynamically create a custom XML property of webpart. Do I have to create a instance of a editor class or something ?

well confused and stuck !


Try to use the CDATA tag: http://www.w3schools.com/xml/xml_cdata.asp. If that doesn't work, do the following:

  • add the webpart manually
  • run code to get the webpartmanager and all webparts on the page
  • for the webpart, check all properties, and see what the zonetabxml looks like

what you need, is to insert unparsed character data into your webpart.

for xmldocuments, this can be done with: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection(v=vs.90).aspx. (tbh, this is not a xml document, but you can make smart use of it to insert the generated cdata string into your webpart property)

| improve this answer | |
  • If i add the webpart manually then i can simply change the properties using "Modify Webpart Properties, however here i have to add webpart to hundred of pages so that aint option here. the w3school link you have given me, tells more like using C# code in an XML file then using XML code in c# :S, i guess/ – Muhammad Raja Apr 12 '12 at 12:34
  • oops, it was the wrong link. I updated my answer – Bas Lijten Apr 13 '12 at 14:16
  • +1 for answer but it didn't really helped me that much :) – Muhammad Raja Apr 14 '12 at 7:01

easy answers, replace ">" with > and "<" with < and simply assign the XML lines of code to a string, in order to use "hello" double quotes need to do it this way \"hello\" and assign the string to the Webpart property. took me ages to figure this out.


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.