Luis Valencia
Luis Valencia

Reputation: 33998

How to add a listviewwebpart by code

In my current production code, we have the following that was working perfectly fine:

XsltListViewWebPart clientInfoWebPart = new XsltListViewWebPart();
                                    clientInfoWebPart.Title = "Client Info";
                                    clientInfoWebPart.WebId = web.ID;
                                    clientInfoWebPart.ListName = clientInfoListSP.ID.ToString("B").ToUpper();
                                    clientInfoWebPart.ChromeType = PartChromeType.TitleOnly;
                                    clientInfoWebPart.Toolbar = "None";
                                    clientInfoWebPart.XmlDefinition = clientInfoHomePageview.GetViewXml();
                                    clientInfoWebPart.ViewGuid = clientInfoHomePageview.ID.ToString("B").ToUpper();
                                    clientInfoWebPart.Hidden = true;
                                    mgrPageManager.AddWebPart(clientInfoWebPart, "Header", 0);
                                    mgrPageManager.SaveChanges(clientInfoWebPart);

Howeever, after SP1 was installed, we got this issue, in the SaveCHanges line.

(Object reference not set to an instance of an object.) System.NullReferenceException   
 at Microsoft.SharePoint.WebPartPages.BaseXsltListWebPart.EnsureView()    
 at Microsoft.SharePoint.WebPartPages.XsltListViewWebPart.get_View()    
 at Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChangesCore(SPLayoutProperties layoutProperties, Boolean httpGet, Boolean saveCompressed, Boolean skipRightsCheck, Boolean skipSafeAgainstScriptCheck, WebPartTypeInfo& newTypeId, Byte[]& newAllUsersProperties, Byte[]& newPerUserProperties, String[]& newLinks)    
 at Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChangesInternal(SPLayoutProperties layoutProperties, Boolean skipRightsCheck, Boolean skipSafeAgainstScriptCheck)    
 at Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChanges(Guid storageKey)    
 at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.SaveChanges(WebPart webPart)    

Question is, whats happening here? How can I fix it? Do I need the save changes? Why was it working without throwing the exception PRE-SP1 and now its failing!?

Upvotes: 0

Views: 1019

Answers (2)

areve
areve

Reputation: 524

I had the same problem, I notice you posted the same question here and you found a solution.

this didnt fix the problem, however removing the save changes fixed it, apparently, when adding webpart, save changes is not needed (after SP1), code was working PRE-SP1

Upvotes: 0

Chris Cannon
Chris Cannon

Reputation: 1167

Try setting the ListId in the code to the GUID of the list, then you shouldn't need to bother messing around with ListName. Also shouldn't ViewGuid be a GUID and not a string?

Also try setting ListId query string of page e.g.

SomePage.aspx?ListId=GUID

Replace GUID with the GUID of the list.

How is the ListItemId set?

Upvotes: 1

Related Questions