CAMLViewFields usage

Mar 8, 2011 at 11:46 PM

Hello,

I am trying to limit my result set to a couple of fields using the CAMLViewFields option, but instead I'm getting all fields back in the response.  What am I doing wrong?

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "MyListName",
    CAMLViewFields: "<ViewFields>" +
        "<FieldRef Name='ID'/>" +
        "<FieldRef Name='Title'/>" +
        "</ViewFields>",
    CAMLRowLimit: 100,
    completefunc: function (data, status) {
        if (status === "success") {
            $(data.responseXML).find("[nodeName='z:row']").each(function () {
                // All fields are showing instead of just ID and Title upon inspection of row :(               
            });
        }
    }
});

Mar 9, 2011 at 7:48 PM

This will depend on what rows you are referring to. I believe it will always return all required fields regardless of your viewfields. If you are getting fields that are not required, that could be different. 

Coordinator
Mar 9, 2011 at 9:11 PM

There's actually a bug (as far as I can tell) in GetListItems. There's a base set of column names which are always returned, regardless what you specify in the viewFields. There's an attribute you are supposed to be able to specify in the queryOptions (IncludeMandatoryColumns - See http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx) to negate this, but it doesn't work. I'm trying to work with Microsoft to figure out either how to fix this or at least fix the documentation so that it is correct.

M.

Mar 28, 2011 at 4:21 PM

Marc,

I'm running into this issue as well on a project that I am working with where the inability to turn of these fields is causing a parser error on the XML returned from the web service. Here's the code:

 

var Query_ID = "<Query><OrderBy><FieldRef Name=\"ID\"/></OrderBy></Query>";
var ViewFields_ID = "<ViewFields><FieldRef Name=\"ID\"/></ViewFields>";
var Query_Begin = "<Query><Where><Contains><FieldRef Name=\"Doc_Department\" /><Value Type=\"DualLookup\">";
var Query_End = "</Value></Contains></Where><OrderBy><FieldRef Name=\"doc_id\" Ascending=\"False\" /></OrderBy></Query>";
var Query_Options = "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>";

function GetDocumentLibraryTotalCount(libraryGUID, countContainer)
{
	var counting = $(countContainer);
	var CurrentSite = counting.attr("rel");
	var Query = Query_Begin + xmlencode(CurrentSite) + Query_End;
	$(countContainer).html("<img src=\"/sites/policy/Page/images/loader.3d.thinarrows.10x12.bg-ffffff.fg-ffffff.gif\" alt=\"Counting...\">Counting...");
	$().SPServices({
		operation: "GetListItems",
		listName: libraryGUID,
		CAMLQuery: Query,
		CAMLViewFields: ViewFields_ID,
		CAMLQueryOptions: Query_Options,
		async: true,
		completefunc: function (xData, Status) {
				UpdateTotalItemCount(xData, Status, countContainer);
			}
	});
}

function xmlencode(string) {
    return string.replace(/\&/g,'&'+'amp;').replace(/</g,'&'+'lt;')
        .replace(/>/g,'&'+'gt;').replace(/\'/g,'&'+'apos;').replace(/\"/g,'&'+'quot;');
}

function UpdateTotalItemCount(xData, Status, countContainer)
{
	var itemCount = 0;
	if(Status == "success")
	{
		itemCount = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
		itemCount = parseInt(itemCount);
	} else { itemCount = -1; }
	
	var updateContainer = $(countContainer);
	
	if(itemCount > 0)
	{
		var itemText = itemCount == 0 ? "item" : "items in department";
		updateContainer.html(addCommas(itemCount) + " total " + itemText);
	} else if (itemCount == 0)
	{
		updateContainer.text("No items found for this department.");
	} else {
		updateContainer.text("Query Error!");
	}
}

 

Everything works great until I hit a query that returns illegal XML characters:

XML Parsing Error: reference to invalid character number Location: moz-nullprincipal:{3da9cb2f-1ded-400f-bb9e-3c38e76f6474} Line Number 1579, Column 36:
vti_title:SW|Installation Planning &#19; 

Any idea on how this might be resolved? It appears we have a single invalid XML character returned due to the ows_MetaInfo field that cannot be excluded from the results.

Thanks!

Coordinator
Mar 28, 2011 at 4:39 PM

Have you tried dumping the xData output to $().SPServices.SPDebugXMLHttpResult? This may be as simple as putting provisions in to deal with the special character; SPDebugXMLHttpResult should give you the full data set so you can see the data. I've used GetListItems to pull the MetaInfo field many times.

Coordinator
Mar 28, 2011 at 4:48 PM

Maybe I'm missing it. Where are you even trying to access the MetaInfo values in your script?

M.

Coordinator
Mar 28, 2011 at 4:52 PM

p.s. You need single quotes around rs:data:

find("[nodeName='rs:data']")

M.

Mar 28, 2011 at 5:09 PM

Have you used U2U to hardcode the CAMLquery and then compare that query against your script's invalid CAMLquery?  That may shed some light on how to handle the invalid character.

Mar 28, 2011 at 5:17 PM

Webdes,

I haven't attempted that before, but I'll give it a shot. Thanks!

Marc,

I'm not attempting to use the MetaInfo field, it is returned with my query and seems to be the field that is having the parser error.

iOnline,

I used U2U to generate the original query and then revised it to be dynamic once I had it working.

Coordinator
Mar 28, 2011 at 5:20 PM

I notice the error is

XML Parsing Error: reference to invalid character number Location: moz-nullprincipal:{3da9cb2f-1ded-400f-bb9e-3c38e76f6474} Line Number 1579, Column 36:

Since it's got moz- in front, is this error only occurring in Firefox?

M.

Mar 28, 2011 at 5:26 PM

Webdes,

Here is the output from the debug:

#document

xml-stylesheet href="chrome://global/locale/intl.css" type="text/css"
parsererror XML Parsing Error: reference to invalid character number Location: http://.../sites/policy/_vti_bin/Lists.asmx Line Number 1579, Column 36:

I'm using version 0.5.7 and it appears to be a problem with the title of the document. The document title contains an MS word hyphen, and this appears to be what is breaking it. I'm only querying for the ID field since I'm using this data to count the available documents. If there is a better way to count the documents, I'm open to it. Thanks!

 

Mar 28, 2011 at 5:27 PM

Marc,

No, the error was first detected in IE 7.

Coordinator
Mar 28, 2011 at 5:50 PM

Hmm. If SharePoint is sending back invalid XML, then I'm not sure what to do about it. If you change the Title to not have the MS Word hyphen, that fixes the error?

M.

Coordinator
Mar 28, 2011 at 5:55 PM

As Marc said, it sounds like SharePoint is sending back malformed XML (my assumption is that the MS word hyphen doesn't get properly escaped by the web service. You can attempt to verify that is the issue by replicating the query with the U2U tool, or with the .NET web service studio (http://webservicestudio.codeplex.com/).

Mar 28, 2011 at 6:18 PM

I also remember reading a post of yours where Christophe mentioned an OOTB encode function: STSHtmlEncode.  What happens if you use that instead of your xmlencode function?

http://devspoint.wordpress.com/2011/01/07/two-quick-javascript-snippets-i-use-everyday/

Mar 28, 2011 at 6:43 PM

WebDes03,

The U2U CAML builder is also displaying an XML parsing error, so it is definitely happening server side. Looks like I discovered a new "bug" -- now to find a resolution with the client.

We were able to find the document and are working with the site owner to address the problem. Something interesting to note -- MS word special character "may" cause web-services to fail!

Coordinator
Mar 28, 2011 at 7:58 PM

Actually, did you try putting the single quotes around rs:data? Seems to get rid of the "bug" with my test case.

M.

Mar 29, 2011 at 3:52 PM

No, but I will make that correction. Unfortunately, I no longer have the test case at the moment since the data has been changed.

Coordinator
Mar 29, 2011 at 3:55 PM

Well, with that fix, the test case I had is moot. Maybe I'm not doing exactly the same thing, but I don't see the &#19; anywhere in the returned XML, even when the Title contains the em dash.

M.

Mar 29, 2011 at 6:25 PM

It looks like &#19 is a control code of some kind for standard ASCII -- http://www.ascii-code.com/. I'm not sure how the character got into the title field -- using a Hex editor I placed it into Notepad++ and it is definately a control code. Pasting it into Word results in the unknown character item. I'm puzzled how this got in there and I can understand why Microsoft might not test for it. BTW, this also appears in SharePoint 2010 when we migrated the site collection to our new server.

Sep 15, 2012 at 9:20 PM
Edited Sep 15, 2012 at 9:21 PM
sympmarc wrote:

There's actually a bug (as far as I can tell) in GetListItems. There's a base set of column names which are always returned, regardless what you specify in the viewFields. There's an attribute you are supposed to be able to specify in the queryOptions (IncludeMandatoryColumns - See http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx) to negate this, but it doesn't work. I'm trying to work with Microsoft to figure out either how to fix this or at least fix the documentation so that it is correct.

M.

I agree Marc...!!!

Irrespective of what I specify, I always end up getting these columns as some kind of "minimum set of columns":-

MetaInfo _ModerationStatus _Level ID UniqueId owshiddenversion FSObjType Created PermMask Modified FileRef

Since I get a kind of large # of rows, I wanted to fetch only 3/4 columns but the web service always returns the above set of column (apart from the ones which I want) no matter what I specify.

I guess we just have to live with it (thanks to almighty MSFT)... :)

Coordinator
Sep 17, 2012 at 7:44 PM
Yup. I've never found a way to make the GetListItens operation work as advertised.

M.