8

I am adding a new view to a list using CSOM (ViewTypeKind = Grid).

The same code works perfectly for other types of views.

For datasheet views, although they are added and they function properly, they are only available in the ribbon dropdown, not in the page context menu view selector.

enter image description here

The fact that other people are experiencing this makes me suspect of an issue in the API but perhaps there is something we can do to overcome this.

Adding the view manually through the UI works fine.

Similar thread:

List views of type Datasheet created in CSOM do not appear in the list's 'view selector'

About the View Selector (aka ClientPivotControl):

List View Web part not showing all available views for list

EDIT:

Here's a basic example in case anyone wants to give it a try.

BTW, it appears that this issue is not limited to CSOM. It happens the same when adding datasheet views from the server side.

param(
    $webUrl = "http://yourwebsite",
    $listTitle = "yourlisttitle",
    $viewTitle = "datasheet-test"
)

$web = get-spweb $webUrl
$list = $web.Lists[$listTitle]
$view = $list.Views.Add($viewTitle, "Name", "", 50, $true, $false, "GRID", $false)
2
  • What do u mean by contextual menu?
    – Taran Goel
    Commented Oct 5, 2016 at 14:48
  • in every list view/page there is a control next to the search box called ms-pivotControl. this control is responsible for displaying some of the views in that area and there is a 3 dot context menu to pick more views. this is done by some scripts in clienttemplates.js Commented Oct 5, 2016 at 14:54

3 Answers 3

2
+100

Hmmm, i tried the below mentioned code and it works. It's created in CSOM.

List taskList = currentWeb.Lists.GetByTitle("Tasks");

                // Get all the view  
                //for the custom list  
                ViewCollection viewColl = taskList.Views;

                // Specify the columns that should be displayed
                string[] viewFields = { "ID", "LinkTitle", "StartDate", "DueDate", "AssignedTo", "Status", "Body" };

                // Specifies the properties used to create a new list view    

                ViewCreationInformation creationInfo = new ViewCreationInformation();
                creationInfo.Title = "All my custom Tasks";
                creationInfo.RowLimit = 30;
                creationInfo.ViewFields = viewFields;
                creationInfo.ViewTypeKind = ViewType.Grid;
                creationInfo.SetAsDefaultView = false;
                viewColl.Add(creationInfo);
                context.ExecuteQuery();

enter image description here

9
  • sure it appears in view selector (while a different view is open)? what specs/versions? Commented Oct 6, 2016 at 12:57
  • @TiagoDuarte Yes, this is production ready code. The csom dll version number is 16.0.2617.1200 Commented Oct 6, 2016 at 14:57
  • it's possible that it works in Office 365. but this question is specific for 2013 Commented Oct 6, 2016 at 15:23
  • Yep, looks like 2013 specific issue. Hopefully might get fixed in future updates. However , i was able to get it working after a one time activity. I clicked on edit page > edit webpart > disable view selector menu(tick checkbox) > OK. Then again edit webpart > disable view selector menu > uncheck the checkbox > Ok > Save page. After doing this, i was able to get it working. Commented Oct 6, 2016 at 16:21
  • check above screenshot for "Disable view selector menu". Commented Oct 6, 2016 at 16:28
2

This solution works for me with update ViewTypeKind like creationInfo.ViewTypeKind = ViewType.Grid | ViewType.Html; using CSOM

     List taskList = currentWeb.Lists.GetByTitle("Documents");

        // Get all the view  
        //for the custom list  
        ViewCollection viewColl = taskList.Views;

        // Specify the columns that should be displayed
        string[] viewFields = { "ID", "LinkFilename", "Title", "Editor" };

        // Specifies the properties used to create a new list view    

        ViewCreationInformation creationInfo = new 
        ViewCreationInformation();
        creationInfo.Title = "DataSheet by Selva";
        creationInfo.RowLimit = 30;
        creationInfo.ViewFields = viewFields;
        creationInfo.ViewTypeKind = **ViewType.Grid | ViewType.Html;**
        creationInfo.SetAsDefaultView = false;
        viewColl.Add(creationInfo);
        ctx.ExecuteQuery();
1
  • Thanks @MHeld for updated with right format. Hereafter I will take care.
    – Selva R
    Commented Jun 1, 2018 at 9:50
1

So time is running out and I don't see any solution for this.

User gautam pointed me in the right direction, which is why I am giving him the bounty, but I want to post the actual answer myself.

This is clearly a bug in the SP2013 version of the view selector functionality, which is setup in the actual webpart, not the view.

I'd also like to point out that this issue is not limited to SSOM or CSOM, it happens in both and it also happens in various types of views, such as Datasheet and Calendar, probably others.

The workaround is to trigger an update on the DisableViewSelectorMenu webpart property, which can be achieved by manually editing the view webpart through the user interface, or programatically updating the property and the webpart, as shown below.

public void FixWebPartViewSelector(ClientContext context, string listTitle, string viewFileName)
{
    Web web = context.Web;
    List list = web.Lists.GetByTitle(listTitle);
    context.Load(list, l => l.RootFolder);
    context.ExecuteQuery();

    string pageUrl = list.RootFolder.ServerRelativeUrl + "/Forms/" + viewFileName;

    Console.WriteLine("Fixing view selector in: " + pageUrl);

    File file = web.GetFileByServerRelativeUrl(pageUrl);
    PersonalizationScope scope = Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared;
    LimitedWebPartManager wpm = file.GetLimitedWebPartManager(scope);

    context.Load(wpm);
    context.Load(wpm.WebParts);
    context.ExecuteQuery();

    int numWebparts = wpm.WebParts.Count;

    Console.WriteLine("Found " + numWebparts + " webparts!");
    if(numWebparts == 1)
    {
        WebPartDefinition wpd = wpm.WebParts[0];
        WebPart wp = wpd.WebPart;

        //PrintWebpartProperties(context, wp);

        //update view selector
        wp.Properties["DisableViewSelectorMenu"] = false;

        //commit
        wpd.SaveWebPartChanges();
        context.ExecuteQuery();
    }    
}

USAGE:

FixWebPartViewSelector(context, "Documents", "AllItems.aspx");

See also

(Calendar) list views disappear from view selector menu of list view webpart

Programmatically created calendar view does not appear on view selector menu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.