locked
How to change the order of the webpart on sharepoint 2010 page through programatically? RRS feed

  • Question

  • I need to change the order of the existing webparts on sharepoint 2010 page through programatically.

    Here I'm able to get all webpart properties, ZoneID is showing for all webparts "WPZ" but Zone Index is changing like for every webpart 2,1,0. when I'm changing zoneindex webparts are not updating still webparts is in same place on sharepoint page.

    Note:ZoneID and Zoneindex textboxes Inactive mode in webpart propertis. 


    Monday, December 24, 2012 8:10 AM

Answers

  • Ravi,

    You cannot add webpart zone since it needs to be part of page layout at least not a good practice. But you can add webpart to rich content of wiki page using the below snippet and specify the order accordingly

    using (SPSite site = new SPSite("http://win2008/sites/publishing"))
    {
        SPWeb web = site.RootWeb;
        SPFile page = web.GetFile("Pages/Lipsum.aspx");
        page.CheckOut();
     
        using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
        {
            Guid storageKey = Guid.NewGuid();
            string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_'));
     
            XmlElement p = new XmlDocument().CreateElement("p");
            p.InnerText = "Hello World";
            ContentEditorWebPart cewp = new ContentEditorWebPart
            {
                Content = p,
                ID = wpId
            };
            wpmgr.AddWebPart(cewp, "wpz", 0);
     
            string marker = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>", new object[] { storageKey.ToString("D") });
            SPListItem item = page.Item;
            string content = item["PublishingPageContent"] as string;
            item["PublishingPageContent"] = content.Replace("|", marker);
            item.Update();
        }
     
        page.CheckIn(String.Empty);
    }

    For more details check out the below post

    http://blog.mastykarz.nl/programmatically-adding-web-parts-rich-content-sharepoint-2010/


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    • Marked as answer by Qiao Wei Thursday, January 3, 2013 1:57 AM
    Thursday, December 27, 2012 7:49 AM
  • You can also check this post i found you can also try using this 

    http://dbremes.wordpress.com/2010/06/06/adding-web-parts-to-sharepoint-2010-wiki-pages-using-the-client-object-model/


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.


    Thursday, December 27, 2012 7:52 AM

All replies

  • If you are trying to do this in a wiki page it will be in inactive mode. check this link

    http://sharepoint.stackexchange.com/questions/35582/why-is-zone-index-disabled-for-some-web-parts.

    If not then you can specify the web part zone id and idex as below

    ///
    <summary> /// Adds a web part to a web page into the given web part zone ID and the given zone index
    /// </summary>
    ///Web Site to add the web part to
    ///Web page to add the web part to
    ///Web part to add to the given web
    ///Web part zone to add the web part to
    ///The index at which the web part should be added to the web part zone.  Defines order
    public static void AddWebPart(SPWeb web, string page, System.Web.UI.WebControls.WebParts.WebPart webPart, string zoneID, int zoneIndex)
    {
    	SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(web.Url + "/" + page, PersonalizationScope.Shared);
    
    	webPartManager.AddWebPart(webPart, zoneID, zoneIndex);
    	webPartManager.SaveChanges(webPart);
    
    	web.Update();
    
    }
    


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Monday, December 24, 2012 8:25 AM
  • Hi Raghavendra,

      Here I want to move Existing weparts, I have 3 webparts(webpart1,webpart 2,webpart 3) on same page zoneID is same for all webparts "wpz" ,Now I need to display webparts like webpart 3,webpart 2,webpart1.

    note: when i'm fetching webpart properties through sharepoint webservices zoneID is showing "wpz"

    when I fetching webpart properties through sharepoint object model zoneID is showing Null

     I'm executing below code successfully but webparts are not moving on page

    webPartManager.Movewebpart(webPart, "wpz", 1);
    	webPartManager.SaveChanges(webPart);
    	web.Update();


    Monday, December 24, 2012 10:22 AM
  •  If you are using a WebPart page then using the WebPart Manager class you can programmatically change the order of the webparts on the page.

    webPartManager.Movewebpart(webPart,"wpz",1);

    webPartManager.SaveChanges(webPart);

    web.Update();

    Here in the First line WebPart with the Title wpz with the Zone ID 1. It will be placed in the front.

    But in the Wiki page the Zone Id field gets disabled automatically.

    Hope this will help.


    Sharepoint Sutra
    Sharepoint Consultant

    Tuesday, December 25, 2012 6:48 AM
  • Hi,

      Thanks for your reply, Same code I tried you can see my above reply,but webpart position is not changing on my page,If i change ZoneID(WPZ) then only its changing.

    Tuesday, December 25, 2012 7:02 AM
  • Hi Ravi

    get the webparts from the page

    delete the webparts

    add them in the required order

    using the code above.

    regards

    manish


    Tuesday, December 25, 2012 9:07 AM
  • Hi,

     I already tried what you have suggested,below is my code

    SPLimitedWebPartManager wpm;
    wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
               
    foreach(System.Web.UI.WebControls.WebParts.WebPart webpart in wpm.WebParts)
    {
     if (webpart.Title == "Webpart1")
      {
                        
        wpm.DeleteWebPart(webpart);
        wpm.AddWebPart(webpart, "Topzone", zoneIndex);
        wpm.SaveChanges(webpart);
        web.Update();
       }
    }
    After exceuting the above code webpart added on bottom of the page but not added on the top of the page while passing zoneID as "TopZone" or "Header". and webpart is not showing after edit the page


    • Edited by T Ravi Kumar Tuesday, December 25, 2012 10:30 AM
    Tuesday, December 25, 2012 10:22 AM
  • Hi,

    Yes it is wiki page,  In sharepoint 2010 teamsite default page is wikipage that means Richtext editor, we dont want webpart zones to add webparts,so webparts properties ZoneID and Zoneindex are disabled.

    But Is there any way to enable ZoneIndex and ZoneID for webparts on wiki page, OR is there any way to move webparts with in same zone(on wiki page having only one webpar zone "wpz",it is in Hidden)

    Thursday, December 27, 2012 7:33 AM
  • Ravi,

    You cannot add webpart zone since it needs to be part of page layout at least not a good practice. But you can add webpart to rich content of wiki page using the below snippet and specify the order accordingly

    using (SPSite site = new SPSite("http://win2008/sites/publishing"))
    {
        SPWeb web = site.RootWeb;
        SPFile page = web.GetFile("Pages/Lipsum.aspx");
        page.CheckOut();
     
        using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
        {
            Guid storageKey = Guid.NewGuid();
            string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_'));
     
            XmlElement p = new XmlDocument().CreateElement("p");
            p.InnerText = "Hello World";
            ContentEditorWebPart cewp = new ContentEditorWebPart
            {
                Content = p,
                ID = wpId
            };
            wpmgr.AddWebPart(cewp, "wpz", 0);
     
            string marker = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>", new object[] { storageKey.ToString("D") });
            SPListItem item = page.Item;
            string content = item["PublishingPageContent"] as string;
            item["PublishingPageContent"] = content.Replace("|", marker);
            item.Update();
        }
     
        page.CheckIn(String.Empty);
    }

    For more details check out the below post

    http://blog.mastykarz.nl/programmatically-adding-web-parts-rich-content-sharepoint-2010/


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    • Marked as answer by Qiao Wei Thursday, January 3, 2013 1:57 AM
    Thursday, December 27, 2012 7:49 AM
  • You can also check this post i found you can also try using this 

    http://dbremes.wordpress.com/2010/06/06/adding-web-parts-to-sharepoint-2010-wiki-pages-using-the-client-object-model/


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.


    Thursday, December 27, 2012 7:52 AM