Probably this is something you want to do all the time because when you add a ListViewWebPart to a page, in the backgound SharePoint will generate a new hidden view in the views collection of the corresponding list.
Unfortunately, this new view isn't a copy of the current default view of the list...
As always, to work fast, I'm using the OfficeAMS extension methods. Since my last post, things have changed for OfficeAMS, as they say themselves: they've grown up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties) | |
{ | |
var result = new SPRemoteEventResult(); | |
using (var clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties)) | |
{ | |
if (clientContext != null) | |
{ | |
try | |
{ | |
//create document library | |
clientContext.Web.AddDocumentLibrary("Internal documents", true); | |
//create wiki page | |
clientContext.Web.AddWikiPage("Site Pages", "Documents.aspx"); | |
//set correct page layout to wiki page | |
clientContext.Web.AddLayoutToWikiPage("sitepages", WikiPageLayout.TwoColumns, "Documents"); | |
//get new document library | |
var internallDocLib = clientContext.Web.GetListByTitle("Internal documents"); | |
//add listviewwebparts to page | |
var internalDocsWebPartEntity = new WebPartEntity | |
{ | |
WebPartIndex = 2, | |
WebPartTitle = "Latest Internal Documents", | |
WebPartZone = "Left", | |
WebPartXml = string.Format(Globals.ListViewWebPart, internallDocLib.Id, "Latest Internal Documents") | |
}; | |
//params: pageslibrary url, webpartentity, wiki page, table row, table column, addSpace under web part | |
clientContext.Web.AddWebPartToWikiPage("sitepages", internalDocsWebPartEntity, "documents.aspx", 1, 1, true); | |
//adapt hidden views used by xsltlistviewwebparts | |
clientContext.Web.Context.Load(internallDocLib.Views); | |
clientContext.Web.Context.ExecuteQuery(); | |
//use linq query to find the view where the view its property "ServerRelativeUrl" is equal to the page url that contains the web part. | |
var internalViewHomePage = | |
(from v in internallDocLib.Views | |
where v.ServerRelativeUrl == String.Format("{0}/SitePages/{1}", clientContext.Web.ServerRelativeUrl, "documents.aspx") | |
select v).SingleOrDefault(); | |
//remove all viewfields and add desired columns. | |
if (internalViewHomePage != null && internalViewHomePage.Hidden) | |
{ | |
internalViewHomePage.ViewFields.RemoveAll(); | |
internalViewHomePage.ViewFields.Add("DocIcon"); | |
internalViewHomePage.ViewFields.Add("LinkFilename"); | |
internalViewHomePage.ViewFields.Add("_UIVersionString"); | |
internalViewHomePage.ViewFields.Add("Modified"); | |
internalViewHomePage.ViewFields.Add("Editor"); | |
internalViewHomePage.Update(); | |
internalViewHomePage.Context.ExecuteQuery(); | |
} | |
} | |
catch | |
{ | |
//write logging information | |
} | |
} | |
} | |
} |
No comments:
Post a Comment