Suppose in a zone there are two webparts indexing 1 and 2 and now we have to add a new webpart programmatically in between these two webparts then we have to give index value 2 to new value and previous 2 will automatically become 3.
CODE:
1: foreach (PublishingPage pubpge in publishingPages)
2:
3: {
4:
5: SPFile pageCategory = pubpge.ListItem.File;
6:
7: SPLimitedWebPartManager spmgr = sourceProductsWeb.GetLimitedWebPartManager(pageCategory.Url.ToString(), PersonalizationScope.Shared);
8:
9: string exportedWebPartXml = string.Empty;
10:
11: exportedWebPartXml = new StringReader(sourceSite.RootWeb.GetFileAsString(sourceSite.RootWeb.Url + "/_catalogs/wp/EngagementProductLinks.webpart")).ReadToEnd();
12:
13: XmlTextReader reader = new XmlTextReader(new StringReader(exportedWebPartXml));
14:
15: System.Web.UI.WebControls.WebParts.WebPart importedWp = spmgr.ImportWebPart(reader, out outmessage);
16:
17: sourceProductsWeb.AllowUnsafeUpdates = true;
18:
19: if (pageCategory.CheckOutType == SPFile.SPCheckOutType.None)
20:
21: {
22:
23: pageCategory.CheckOut();
24:
25: }
26:
27: importedWp.ChromeType = PartChromeType.None;
28:
29: spmgr.AddWebPart(importedWp, "ContactsZone", 1);
30:
31: spmgr.SaveChanges(importedWp);
32:
33: pageCategory.CheckIn(" Added Web Part " + importedWp.Title);
34:
35: pageCategory.Publish(" Added Web Part " + importedWp.Title);
36:
37: if (pubpge.ListItem.ModerationInformation != null && (pubpge.ListItem.ModerationInformation.Status == SPModerationStatusType.Draft || pubpge.ListItem.ModerationInformation.Status == SPModerationStatusType.Pending))
38:
39: {
40:
41: pageCategory.Approve("Programmatically Approve");
42:
43: }
44:
45: sourceProductsWeb.AllowUnsafeUpdates = false;
If there are already three webparts and we have to fourth webpart then increment of webpart index will be 1. Now zone has four webpart and want to add fifth webpart it will be added at correct place but index of webpart will not be increased by one. They will increase by multiple of index like if 2 then it will increase by 4.
Reference for Limitation : http://blogs.msdn.com/b/jjameson/archive/2009/06/05/splimitedwebpartmanager-addwebpart-mysteriously-increments-zoneindex.aspx
http://www.technologytoolbox.com/blog/jjameson/archive/2009/06/05/splimitedwebpartmanager-addwebpart-mysteriously-increments-zoneindex.aspx
Reference for ordering webpart: http://nikspatel.wordpress.com/2010/11/09/programmatically-add-the-web-part-on-the-sharepoint-web-part-page/
Also we can add property value programmatically.
Like i have added custom property(Taxonomy field) value while adding webparts on page.
By using "SetValue" property. CODE:
1: foreach (PropertyInfo sitesection in SiteSecitonProperty)
2:
3: {
4:
5: if (sitesection.Name == "SiteSectionTaxonomy")
6:
7: {
8:
9: sitesection.SetValue(importedWp, GUID, null);
10:
11: spmgr.SaveChanges(importedWp);
12:
13: }
14:
15: else if (sitesection.Name == "TermStoreID")
16:
17: {
18:
19: string TermStoreguid = ContentDeploymentConfigObj.TaxonomyStoreID;
20:
21: System.Guid guid = new Guid(TermStoreguid);
22:
23: sitesection.SetValue(importedWp, guid, null);
24:
25: spmgr.SaveChanges(importedWp);
26:
27: }
28:
29: else if (sitesection.Name == "TermSetID")
30:
31: {
32:
33: string TermSetguid = ContentDeploymentConfigObj.TaxonomySetID;
34:
35: System.Guid guid = new Guid(TermSetguid);
36:
37: sitesection.SetValue(importedWp, guid, null);
38:
39: spmgr.SaveChanges(importedWp);
40:
41: }
42:
43: else if (sitesection.Name == "Text")
44:
45: {
46:
47: sitesection.SetValue(importedWp, GUID, null);
48:
49: spmgr.SaveChanges(importedWp);
50:
51: }
52:
53: }
If you have any questions you can reach out our SharePoint Consulting team here.