Share via


ViewCollection.Add method

Adds a new list view to the collection.

Namespace:  Microsoft.SharePoint.Client
Assembly:  Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)

Syntax

'Declaration
Public Function Add ( _
    parameters As ViewCreationInformation _
) As View
'Usage
Dim instance As ViewCollection
Dim parameters As ViewCreationInformation
Dim returnValue As View

returnValue = instance.Add(parameters)
public View Add(
    ViewCreationInformation parameters
)

Parameters

Return value

Type: Microsoft.SharePoint.Client.View
Returns a View instance representing the addition of a new list view to the collection.

Exceptions

Exception Condition
[System.ArgumentException]

Field internal name for field is not in the list view. Error code: -2147024809.

[System.InvalidOperationException]

Type of the new list view is not allowed for the list. Error code: -1.

[System.IO.DirectoryNotFoundException]

List does not exist. Error code: -2147024893.

[System.UnauthorizedAccessException]

The current user has insufficient permissions. Error code: -2147024891.

Remarks

It must not be a null reference (Nothing in Visual Basic).

Examples

This code example adds a new view to the Tasks list of the specified site, and displays the list’s current views.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class ViewCollection_AddExample
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;

            List targetList = site.Lists.GetByTitle("Tasks");
            ViewCollection collView = targetList.Views;

            ViewCreationInformation viewInfo = new ViewCreationInformation();
            viewInfo.Title = "MyView";
            collView.Add(viewInfo);

            clientContext.Load(collView);
            clientContext.ExecuteQuery();

            Console.WriteLine("Tasks list current views:\n\n");
            foreach (View oneView in collView)
               Console.WriteLine(oneView.Title); 
        }
    }
}

See also

Reference

ViewCollection class

ViewCollection members

Microsoft.SharePoint.Client namespace