none
Add XsltListViewWebPart via code RRS feed

  • Question

  • We need to add XsltListViewWebPart via code on a publishing page. XsltListViewWebPart should shows Discussion Board.

    Any help?


    http://farhanfaiz.wordpress.com
    Saturday, July 23, 2011 5:16 PM

Answers

  • You can add a webpart using the following code:
    using (SPSite site = new SPSite("http://win2008/sites/publishing"))
    {  
     
    SPWeb web = site.RootWeb;  
     
    SPFile page = web.GetFile("Pages/YourPage.aspx");  
     
    page.CheckOut();   
     
    using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))  
     
    {    
    
      XsltListViewWebPart lvwp = new XsltListViewWebPart();
      lvwp.ListId = list.ID;
      lvwp.ViewGuid = list.Views[viewName].ID.ToString("B").ToUpper(CultureInfo.InvariantCulture);
      lvwp.AllowClose = false;
      lvwp.AllowConnect = false;
      lvwp.AllowEdit = false;
      lvwp.AllowHide = false;
      lvwp.AllowMinimize = false;
      lvwp.AllowZoneChange = false;
      lvwp.Description = list.Description;
      lvwp.Title = webPartTitle;
      lvwp.ChromeType = chromeType;
      
    //wpmgr.AddWebPart(lvwp, zoneId, zoneIndex);
      
    wpmgr.AddWebPart(lvwp, "Header", 0);  
      
    }   
    
      page.CheckIn(String.Empty);
    }
    

    Please remember to mark as answered, if applicable. -- With Regards Shailen Sukul Entrepreneur/Software Architect/Developer/Consultant/Trainer (BSc | Mct | Mcpd (.Net 2/3.5/SharePoint2010) | Mcts (Sharepoint 2010/MOSS/WSS), Biztalk, Web, Win, Dist Apps) | Mcitp(SharePoint) | Mcsd.NET | Mcsd | Mcad) MSN | Skype | GTalk Id: shailensukul Twitter: http://twitter.com/shailensukul Ph: +1 916 359-9557 Website: http://sukul.org Blog: http://shailen.sukul.org/ http://www.linkedin.com/in/shailensukul
    • Marked as answer by malikfarhan Sunday, July 24, 2011 9:48 AM
    Saturday, July 23, 2011 6:51 PM

All replies

  • You can add a webpart using the following code:
    using (SPSite site = new SPSite("http://win2008/sites/publishing"))
    {  
     
    SPWeb web = site.RootWeb;  
     
    SPFile page = web.GetFile("Pages/YourPage.aspx");  
     
    page.CheckOut();   
     
    using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))  
     
    {    
    
      XsltListViewWebPart lvwp = new XsltListViewWebPart();
      lvwp.ListId = list.ID;
      lvwp.ViewGuid = list.Views[viewName].ID.ToString("B").ToUpper(CultureInfo.InvariantCulture);
      lvwp.AllowClose = false;
      lvwp.AllowConnect = false;
      lvwp.AllowEdit = false;
      lvwp.AllowHide = false;
      lvwp.AllowMinimize = false;
      lvwp.AllowZoneChange = false;
      lvwp.Description = list.Description;
      lvwp.Title = webPartTitle;
      lvwp.ChromeType = chromeType;
      
    //wpmgr.AddWebPart(lvwp, zoneId, zoneIndex);
      
    wpmgr.AddWebPart(lvwp, "Header", 0);  
      
    }   
    
      page.CheckIn(String.Empty);
    }
    

    Please remember to mark as answered, if applicable. -- With Regards Shailen Sukul Entrepreneur/Software Architect/Developer/Consultant/Trainer (BSc | Mct | Mcpd (.Net 2/3.5/SharePoint2010) | Mcts (Sharepoint 2010/MOSS/WSS), Biztalk, Web, Win, Dist Apps) | Mcitp(SharePoint) | Mcsd.NET | Mcsd | Mcad) MSN | Skype | GTalk Id: shailensukul Twitter: http://twitter.com/shailensukul Ph: +1 916 359-9557 Website: http://sukul.org Blog: http://shailen.sukul.org/ http://www.linkedin.com/in/shailensukul
    • Marked as answer by malikfarhan Sunday, July 24, 2011 9:48 AM
    Saturday, July 23, 2011 6:51 PM
  • @ Shailen - Thanks. Worked like a charm :)


    http://farhanfaiz.wordpress.com
    Sunday, July 24, 2011 9:50 AM
  • Great sample, saved me a lot of time!
    Tuesday, July 3, 2012 11:20 AM