AddView

Jul 8, 2013 at 9:45 PM
Edited Jul 8, 2013 at 10:02 PM
I am trying to use the AddView method.
$().SPServices({
  operation: "AddView",
  webURL: "cannot show the URL, sorry",
  listName: "I am using the GUID list name",
  viewName: "Test1", 
  viewFields: "<FieldRef Name=\"Title\" /><FieldRef Name=\"Document Type\" />", 
  query: "<Where><Lt><FieldRef Name=\"Document Type\" /><Value>\"Transcript\"</Value></Lt></Where>", 
  rowLimit: "10",
  type: "HTML",
  makeViewDefault: false
});
It steps through all the coding fine, however, the end result is not that of an added view to my list. Currently calling jquery.SPServices-2013.01.js and jquery-1.8.2.js

And here is the xml that is generated from the function:
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
  <soap:Body>
    <AddView xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
      <listName>{50D76E22-126B-46AE-BAA2-9ABDA01AAC93</listName>
      <viewName>Test1</viewName>
      <viewFields>
        <FieldRef Name="Title"/>
        <FieldRef Name="Document Type"/>
      </viewFields>
      <query>
        <Where>
          <Lt>
            <FieldRef Name="Document Type"/>
            <Value>"Transcript"</Value>
          </Lt>
        </Where>
      </query>
      <rowLimit>10</rowLimit>
      <type>HTML</type>
      <makeViewDefault>false</makeViewDefault>
    </AddView>
  </soap:Body>
</soap:Envelope>
Here is what my site says the SOAP request should be:
POST /_vti_bin/Views.asmx HTTP/1.1
Host: "CANNOT SHOW URL TO THIS THREAD, sorry"
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/AddView"


<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddView xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <listName>string</listName>
      <viewName>string</viewName>
      <viewFields>string</viewFields>
      <query>string</query>
      <rowLimit>string</rowLimit>
      <type>string</type>
      <makeViewDefault>boolean</makeViewDefault>
    </AddView>
  </soap:Body>
</soap:Envelope>
I also get the following after the script completes from the error console (using google chrome to step through js):

POST "list url that I can't show you, sorry"/_vti_bin/Views.asmx 500 (Internal Server Error) - jquery-1.8.2.js:8416
jQuery.ajaxTransport.send - jquery-1.8.2.js:8416
jQuery.extend.ajax - jquery-1.8.2.js:7968
$.fn.SPServices - jquery.SPServices-2013.01.js:1216
(anonymous function) - Home.aspx:707
Coordinator
Jul 9, 2013 at 3:07 AM
What do you see in the return value?

I see one thing that looks odd in your request, and that's the Document Type < Transcript bit. Does that make sense for your view?

In any case, if you look at the return value, you should see some info about what the issue is.

M.
Jul 9, 2013 at 2:59 PM
Edited Jul 9, 2013 at 3:01 PM
So I've updated my arguments to
$().SPServices({
    operation: "AddView",
    listName: "{50D76E22-126B-46AE-BAA2-9ABDA01AAC93}",
    viewName: "Test1", 
    viewFields: "<ViewFields><FieldRef Name=\"Title\" /><FieldRef Name=\"Document Type\" /></ViewFields>", 
    query: "<Query><Where><Eq><FieldRef Name=\"Document_x0020_Type\" /><Value>Transcript</Value></Eq></Where></Query>", 
    rowLimit: "<RowLimit paged=\"True\">100</RowLimit>",
    type: "HTML",
    makeViewDefault: false
});
A View is created with the following response from /_vti_bin/Views.asmx
<soap:Body>
    <AddViewResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
        <AddViewResult>
            <View Type="HTML" Url="Lists/TestList/Test1.aspx" Personal="FALSE" DisplayName="Test1" DefaultView="FALSE" Name="{E353B741-18C4-45D5-B0C9-1EC778A31281}">
                &gt;<Query>
                    <Where>
                        <Eq>
                            <FieldRef Name="Document_x0020_Type" />
                            <Value>Transcript</Value>
                        </Eq>
                    </Where>
                </Query>
                <ViewFields>
                    <FieldRef Name="Title" />
                    <FieldRef Name="Document_x0020_Type" />
                </ViewFields>
                <RowLimit Paged="TRUE">100</RowLimit>
            </View>
        </AddViewResult>
    </AddViewResponse>
</soap:Body>
However, if I go to /lists/testlist/test1.aspx, I get the following error: Exception from HRESULT: 0x80131904
If I then edit the view and click OK (without changing anything about the view), the view works. My concern is that it might be the "& g t ;" in the respnose right before the <Query> element.

Here is the request
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    <soap:Body>
        <AddView xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
            <listName>{50D76E22-126B-46AE-BAA2-9ABDA01AAC93}</listName>
            <viewName>Test1</viewName>
            <viewFields>
                <ViewFields>
                    <FieldRef Name="Title"/>
                    <FieldRef Name="Document Type"/>
                </ViewFields>
            </viewFields>
            <query>
                <Query>
                    <Where>
                        <Eq>
                            <FieldRef Name="Document_x0020_Type"/>
                            <Value>Transcript</Value>
                        </Eq>
                    </Where>
                </Query>
            </query>
            <rowLimit>
                <RowLimit paged="True">100</RowLimit>
            </rowLimit>
            <type>HTML</type>
            <makeViewDefault>false</makeViewDefault>
        </AddView>
    </soap:Body>
</soap:Envelope>
Coordinator
Jul 9, 2013 at 4:17 PM
I can reproduce this in my test environment running WSS 3.0. The error I get on the view is different, but it's an error just the same:

Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))

I don't see the extra & gt in my response, though.

M.
$().SPServices({
    operation: "AddView",
    listName: "Test List 2",
    viewName: "Test2", 
    viewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='State' /></ViewFields>", 
    query: "<Query><Where><Eq><FieldRef Name='State' /><Value>Massachusetts</Value></Eq></Where></Query>", 
    rowLimit: "<RowLimit paged='True'>100</RowLimit>",
    type: "HTML",
    makeViewDefault: false,
    completefunc: function (xData, Status) {
        alert(xData.responseText);
    }
}); 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <AddViewResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <AddViewResult>
        <View Type="HTML" Url="Lists/Test List 2/Test22.aspx" Personal="FALSE" DisplayName="Test2" DefaultView="FALSE" Name="{03F97D38-70B6-450F-BC00-075600338E3C}">
          <Query>
            <Where>
              <Eq>
                <FieldRef Name="State">
                </FieldRef>
                <Value>Massachusetts</Value>
              </Eq>
            </Where>
          </Query>
          <ViewFields>
            <FieldRef Name="Title">
            </FieldRef>
            <FieldRef Name="State">
            </FieldRef>
          </ViewFields>
          <RowLimit Paged="TRUE">100</RowLimit>
        </View>
      </AddViewResult>
    </AddViewResponse>
  </soap:Body>
</soap:Envelope>
Coordinator
Jul 9, 2013 at 4:34 PM
I did a GetView before and after "fixing" the view, and the results are identical. No idea what's going on here...

M.
Jul 10, 2013 at 1:49 PM
Hi,

I don't know if this is of any help - and please forgive me if it isn't - but I managed to get AddView working fine, using this sample?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>  
 <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2013.01/jquery.SPServices-2013.01.min.js"></script>

<script type="text/javascript">
var divId = "#WSOutput";
var out = "";

// main function
$().ready(function () {

  $().SPServices({
    operation: 'AddView',
    async: false,
    webURL: '<URL of site - no trailing slash>',
    listName: 'Tasks',
    viewName: 'New View',  // docs state that a GUID is required, but a normal view name still works...
    viewFields: '<ViewFields><FieldRef Name="Title" /><FieldRef Name="ID" /></ViewFields>',
    rowLimit: '<RowLimit Paged="True">100</RowLimit>',
    type: 'HTML',
    makeViewDefault: true,
    completefunc: function (xData, Status) {
      $(divId).append("<b>This is the output from the AddView operation:</b><br>");
      $(xData.responseXML).SPFilterNode("AddViewResult").each(function() {
        $(this).SPFilterNode("View").each(function() {
        var theDisplayName = $(this).attr("DisplayName");
        var theDefaultView = $(this).attr("DefaultView");
        var thePersonalView = $(this).attr("Personal");

        if (Status ==  "success") {
          out = "View has been successfully added.<p>";
          out += "Details of view:" + "<br>" +
               "Display name of View: " + theDisplayName + "<br>" +
               "Is it a default view? " + theDefaultView + "<br>" +
               "Is it a personal view? " + thePersonalView + "<br>" +

                 "<hr>";

        } else if (Status == "error") {
          if(strError != "") {          
            out = "Error: " + strError + "<hr>";
          }
        }
      });
     });
     $(divId).append(out);  
    }
  });
});
The only differences I could see between my code and both examples given here, is that:
1) I'm not using a GUID for the viewName (whereas I know the OP's sample did)
2) I'm specifying webURL, which wasn't specified in your code, Marc

The above was originally tested on SP2007, using jQuery 1.7.2 and SPServices 0.71 - I've updated it to 2013 version of SPServices, and jQuery 1.8.2; in both cases I couldn't see any problem. I should point out that it did increment a number on the end of the URL, for each instance of the "New View" set up, but otherwise it produced a series of views for me, with no issue?
Jul 10, 2013 at 2:15 PM
You're also not using a query to define your view. I think that's where I'm running into trouble. Accourding to my response value, I've got this &gt showing up right in front of my query element. I've used the GetViewHtml operation and, despite the error in the response text, the ViewHtml is spot on, yet the view will not function properly unless I go to edit the view and click OK.
Jul 10, 2013 at 2:19 PM
Good point - oops, I didn't spot that, chuckle!

If you remove your query statement - does it produce a view correctly, using the rest of the code as it stands?
Jul 10, 2013 at 2:31 PM
I think I've spotted something else in your rowLimit statement:
rowLimit: "<RowLimit paged=\"True\">100</RowLimit>",
The example given on the MS docs has True in caps - not sure if this is case sensitive?
Jul 10, 2013 at 2:32 PM
Yes, it does work, however, the Query element is critical in my application. I've contacted our SP Admins on the issue.
Jul 10, 2013 at 3:48 PM
I FOUND IT

query: "<Query><Where><Eq><FieldRef Name=\"RegID\" /><Value Type=\"Text\">" + thisView + "</Value></Eq></Where></Query>"

I need to declare the Type attribute of the Value tag within the Query element!!!



Thank you alibby and sympmarc!
Coordinator
Jul 10, 2013 at 3:50 PM
Dang! I thought about that but didn't think it mattered. Great catch!

M.
Coordinator
Jul 10, 2013 at 3:52 PM
p.s. I'm going to add this as an example in the docs.

M.
Jul 10, 2013 at 3:54 PM
Here is the result from the GetViewHtml operation without the Type attribute set
<GetViewHtmlResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <GetViewHtmlResult>
        <View Name="{1FBCBABD-6A12-48FD-836B-2E7DFD69388E}" Type="HTML" DisplayName="010103" Url="/REMOVED BY ME/Forms/0101031.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/dlicon.png">
            <ViewFields>
                <FieldRef Name="Title"/>
                <FieldRef Name="RegID"/>
            </ViewFields>
            <Query>
                <Where>
                    <Eq>
                        <FieldRef Name="RegID"/>
                        <Value>010103</Value>
                    </Eq>
                </Where>
            </Query>
            <RowLimit Paged="TRUE">100</RowLimit>
            <XslLink Default="TRUE">main.xsl</XslLink>
            <Toolbar Type="Standard"/>
            <ParameterBindings>
                <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noitemsinview_doclibrary)"/>
                <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noitemsinview_doclibrary_howto2)"/>
            </ParameterBindings>
        </View>
    </GetViewHtmlResult>
</GetViewHtmlResponse>
After opening the view for Edit and clicking the OK button, the result becomes...

<GetViewHtmlResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <GetViewHtmlResult>
        <View Name="{1FBCBABD-6A12-48FD-836B-2E7DFD69388E}" Type="HTML" DisplayName="010103" Url="/REMOVED BY ME/Forms/0101031.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/dlicon.png">
            <ViewFields>
                <FieldRef Name="Title"/>
                <FieldRef Name="RegID"/>
            </ViewFields>
            <Query>
                <Where>
                    <Eq>
                        <FieldRef Name="RegID"/>
                        <Value Type="Text">010103</Value>
                    </Eq>
                </Where>
            </Query>
            <Aggregations Value="Off"/>
            <RowLimit Paged="TRUE">100</RowLimit>
            <Mobile MobileItemLimit="3" MobileSimpleViewField="Title"/>
            <XslLink Default="TRUE">main.xsl</XslLink>
            <Toolbar Type="Standard"/>
            <ParameterBindings>
                <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noitemsinview_doclibrary)"/>
                <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noitemsinview_doclibrary_howto2)"/>
            </ParameterBindings>
        </View>
    </GetViewHtmlResult>
</GetViewHtmlResponse>
Which is identical to the result of the GetViewHTML operation with the added Type attribute to the AddView operation.
Jul 10, 2013 at 4:06 PM
Sweet - nice one! I had a feeling that it was something to do with your query statement; I was thinking along the lines of checking to make sure you had the right internal field names there, but hey - I'm glad you got it working. ;-)