0 out of 1 rated this helpful - Rate this topic

Views.AddView Method

Windows SharePoint Services 3
Creates a view for the specified list.

Web Service: ViewsWeb Reference: http://<Site>/_vti_bin/Views.asmx
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/AddView", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public XmlNode AddView (
	string listName,
	string viewName,
	XmlNode viewFields,
	XmlNode query,
	XmlNode rowLimit,
	string type,
	bool makeViewDefault
)

Parameters

listName

A string that contains the internal name of the list.

viewName

A string that contains the GUID for the view, which determines the view to use for the default view attributes represented by the query, viewFields, and rowLimit parameters. If this argument is not supplied, the default view is assumed.

viewFields

A ViewFields element that specifies which fields to return in the query and that can be assigned to a System.Xml.XmlNode object, as in the following example:

<ViewFields>
  <FieldRef Name="Title" />
  <FieldRef Name="ID" />
</ViewFields>
query

A Query element containing the query that determines which records are returned and in what order, and that can be assigned to a System.Xml.XmlNode object. For example, the following query returns list items where the ID is less than 3 and orders the items by title:

<Query>
  <Where>
      <Lt>
         <FieldRef Name="ID" />
         <Value Type="Counter">3</Value>
      </Lt>
   </Where>
   <OrderBy>
      <FieldRef Name="Title" />
   </OrderBy>
</Query>
rowLimit

A RowLimit element that specifies the number of items, or rows, to display on a page before paging begins, and that can be assigned to a System.Xml.XmlNode object. The fragment can include the Paged attribute to specify that the view return list items in pages. The following example sets a limit of 100 items per page:

<RowLimit Paged="True">100</RowLimit>
type

A string that specifies whether the view is an HTML view or a Datasheet view. Possible values include HTML and Grid.

makeViewDefault

true to make the view the default view for the list.

Return Value

A Introduction to Collaborative Application Markup Language (CAML) fragment in the following form that contains the view schema and can be assigned to a System.Xml.XmlNode object.
<AddViewResult>
  <View Name="{B5C3250A-1974-49E9-9F61-180F86704434}" 
    DefaultView="TRUE" Type="HTML" DisplayName="All Contacts" 
    Url="Lists/Contacts/AllItems.htm" BaseViewID="1" xmlns=""> 
    <RowLimit Paged="TRUE">100</RowLimit>
    <ViewFields>
      <FieldRef Name="LinkTitle" />
      <FieldRef Name="FirstName" />
      <FieldRef Name="Company" />
      <FieldRef Name="WorkPhone" />
      <FieldRef Name="HomePhone" />
      <FieldRef Name="Email" />
    </ViewFields>
    <Query>
      <OrderBy>
        <FieldRef Name="Title" />
        <FieldRef Name="FirstName" />
      </OrderBy>
    </Query>
    <Aggregations></Aggregations>
    <Formats></Formats>
  </View>
</AddViewResult>

The following code example creates a view for a specified list that returns items in which the value of a DateTime field is greater than a specified date and time. This example requires that a using (C#) or Imports (Microsoft Visual Basic) directive be included for the System.Xml namespace.

Web_Reference_Folder.Views viewService = new Web_Reference_Folder.Views();
viewService.Credentials= System.Net.CredentialCache.DefaultCredentials;

string strQuery = "<Where><Gt><FieldRef Name=\"DateTime_Field\" />" +
   "<Value Type=\"DateTime\">2003-6-10T12:00:00Z</Value>" + "</Gt></Where>" +
   "<OrderBy><FieldRef Name=\"Title\" /></OrderBy>";

string strRowLimit = "50";

string strViewFields = "<FieldRef Name=\"Title\" />" +
"<FieldRef Name=\"DateTime_Field\" />";

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query","");
System.Xml.XmlNode ndRowLimit = xmlDoc.CreateNode(XmlNodeType.Element, "RowLimit","");
System.Xml.XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields","");

ndQuery.InnerXml = strQuery;
ndRowLimit.InnerXml = strRowLimit;
ndViewFields.InnerXml = strViewFields;

XmlNode retNode = viewService.AddView("List_Name", "View_Name", ndViewFields, ndQuery, ndRowLimit ,"HTML", false);
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
View_Name and viewService.Url
The "View_Name" is the GUID of the list view , see the following link:
http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/0f9340bf-4b62-4dd0-a806-f799cf35f9e0/ .

When you call the Web Service from external components, you need add the following code into the above code:
viewService.Url = " http://<Site>/_vti_bin/Views.asmx";
Create Private View?

Can you create a Personal/Private view using SharePoint's native web services (views.asmx).

It seems AddView always creates a Public view. Looks like I might be able to use UpdateView to convert my newly created Public view into a Private view (although the documentation is not clear on this). However, for users with Contributor access (or less) to the site, they do not have permissions to create a Public view, so the AddView method fails with a HTTP 401 error.

So is there any way for a non-admin user to use SharePoint's native web services to create a Private view? (or can this be done via RPC?)

ViewStyle?
How would you specify the ViewStyle for the view?