Frage

Trying to use CSOM to update an existing ListViewWebPart on my SharePoint Online home page. I simply want to add a field to the view or set the view with one already created. What are the properties available to me in WebPartDefinition.webpart.Properties[] ?? Eveything I try says the property isn't available.

        using (var ctx = new ClientContext(url))
        {
            ctx.Credentials = new SharePointOnlineCredentials(nc.UserName, nc.SecurePassword);

            Web web = ctx.Web;
            ctx.Load(ctx.Web);
            ctx.ExecuteQuery();

            var page = ctx.Web.GetFileByServerRelativeUrl(ctx.Web.ServerRelativeUrl + pageUrl);
            LimitedWebPartManager wpMgr = page.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);
            ctx.Load(wpMgr.WebParts);
            ctx.ExecuteQuery();

            WebPartDefinition webPartDef = wpMgr.WebParts[0];
            ctx.Load(webPartDef);
            ctx.ExecuteQuery();

            var newQuery = "<Query><Where><IsNotNull><FieldRef Name = 'ID' /></ IsNotNull></ Where></ Query >";
            webPartDef.WebPart.Properties["XmlDefinition"] = newQuery;
            //webPartDef.WebPart.Properties["SelectedView"] = "HomePage";
            webPartDef.SaveWebPartChanges();
            webPartDef.CloseWebPart();
            ctx.ExecuteQuery();
        }
War es hilfreich?

Lösung

Whenever you add a list view, there is hidden view created in the background.

Now, to modify that view, i.e change the query or add/remove a field, you can modify the below sample code:

using (var ctx = new ClientContext(url))
{
    ctx.Credentials = new SharePointOnlineCredentials(nc.UserName, nc.SecurePassword);

    Web web = ctx.Web;
    ctx.Load(ctx.Web);
    ctx.ExecuteQuery();

    var page = ctx.Web.GetFileByServerRelativeUrl(ctx.Web.ServerRelativeUrl + pageUrl); 
    ctx.Load(page);
    ctx.ExecuteQuery();

    //change according to your list title
    var list = web.Lists.GetByTitle("Custom List");
    ctx.Load(list, l => l.Views);
    ctx.ExecuteQuery();

    var webpartManager = page.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);
    ctx.Load(webpartManager);

    var webparts = webpartManager.WebParts;
    ctx.Load(webparts);
    ctx.ExecuteQuery();

    //specify your view fields, needs to be internal column names
    string[] viewFields = { "Title", "Description" };

    foreach (var webpart in webparts)
    {
        ctx.Load(webpart.WebPart.Properties);
        ctx.ExecuteQuery();

        var propertyValue = webpart.WebPart.Properties.FieldValues;

        // check the webpart title
        if (propertyValue["Title"].Equals("Custom List"))
        {

            var listView = list.Views.GetById(webpart.Id);

            ctx.Load(listView);
            ctx.ExecuteQuery(); 

            //delete all the viewFields and then add your custom fields
            listView.ViewFields.RemoveAll();
            foreach (var viewField in viewFields)
            {
                listView.ViewFields.Add(viewField);
            }

            listView.ViewQuery = "<OrderBy><FieldRef Name='ID' /></OrderBy><Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where>";
            listView.Update();
            ctx.ExecuteQuery(); 
        } 
    }


}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top