0

As you may know, when you create a Community site, it automatically creates a discussion list which is added to /SitePages/Community Home.aspx which looks like so.

enter image description here

I am trying to replace this with another list that I have created programmatically and have it look just like the image above.

This is what I have attempted:

//Get the community homepage.
SPFile communityHomepage = web.GetFile("/SitePages/Community Home.aspx");
SPLimitedWebPartManager webPartManager = communityHomepage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

//Index 0 contains the default discussion board list for a community site template.
webPartManager.WebParts[0].AllowClose = true;

//Close the default list web part.
webPartManager.CloseWebPart(webPartManager.WebParts[0]);
webPartManager.SaveChanges(webPartManager.WebParts[0]); 
//Get my list that I want to replace it with.
SPList discList = web.Lists[discBoardName];

XsltListViewWebPart listViewWebPart = new XsltListViewWebPart();
listViewWebPart.ListId = discList.ID; 
listViewWebPart.ListName = discList.ID.ToString("B").ToUpper();
listViewWebPart.Title = discBoardName;
listViewWebPart.ClientRender = true;
listViewWebPart.Toolbar = "Standard";
SPView view = discList.Views["Subject"];
listViewWebPart.ViewId = int.Parse(view.BaseViewID);
listViewWebPart.XmlDefinition = view.GetViewXml();
webPartManager.AddWebPart(listViewWebPart, "Main", 0);
webPartManager.SaveChanges(listViewWebPart);
discList.Update();
listViewWebPart.Dispose();
webPartManager.Dispose();

The default list webpart view is closed fine, but the one I am trying to add is added outside of the page layout all the way at the bottom and does not look like I want it to.

enter image description here

I have also tried using

string zoneId = webPartManager.GetZoneID(webPartManager.WebParts[0]);

and passing zoneId to AddWebPart(), but that didn't work either. So how do I add this List to the page in the correct location with the correct aesthetic look?

1

Shailen Sukul has a great example posted on the MSDN forums. I suggest you start there first.

https://social.msdn.microsoft.com/Forums/en-US/54d780ea-df2f-408d-9056-cf6f0c27f04e/add-xsltlistviewwebpart-via-code?forum=sharepointdevelopmentprevious

  • The web part aesthetically looks the way I want it to, however, the web part is still not being placed in the correct zone on the page. I would have thought string zoneId = webPartManager.GetZoneID(webPartManager.WebParts[0]); would allow me to place it correctly, but zoneId contains value of "wpz" which does not seem like any specific web part zone. Any thoughts on finding the correct zone for a community page? – KidBatman Aug 4 '15 at 16:38
  • 1
    Figured out the placement. I am adding the webpart to a "Rich Content" webpart so there is a little hacking needed. Please reference blog.mastykarz.nl/… – KidBatman Aug 4 '15 at 20:09

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.