1

I know how to use lists.asmx?op=GetListAndView to get list and view details.

I'm trying to do the same using CSOM. And, to keep it efficient, I wanted to make one call for multiple lists/views. But there is a possibility that a list or view doesn't exist so I should be able to check that for each list/view.

This is what I have so far.

var listsToCheck = [
    { "listName" : "....", "listGUID" : "....", "viewName" : "....", "viewGUID" : "...." },
    { "listName" : "....", "listGUID" : "....", "viewName" : "....", "viewGUID" : "...." },
    { "listName" : "....", "listGUID" : "....", "viewName" : "....", "viewGUID" : "...." },
    { "listName" : "....", "listGUID" : "....", "viewName" : "....", "viewGUID" : "...." },
    { "listName" : "....", "listGUID" : "....", "viewName" : "....", "viewGUID" : "...." }
];

ExecuteOrDelayUntilScriptLoaded(function()
{
    var clientContext = SP.ClientContext.get_current();

    for(var i = 0; i < listsToCheck.length; ++i)
    {
        var listToCheck = listsToCheck[i];

        listToCheck.list = clientContext.get_web().get_lists().getByTitle(listToCheck.listName);
        clientContext.load(listToCheck.list);
        clientContext.load(listToCheck.list, 'EffectiveBasePermissions');

        listToCheck.view = listToCheck.list.get_views().getByTitle(listToCheck.viewName);
        clientContext.load(listToCheck.view);
    }

    clientContext.executeQueryAsync(Function.createDelegate(this, function()
    {
        // SEE NOTE BELOW
        for(var i = 0; i < listsToCheck.length; ++i)
        {
            var listToCheck = listsToCheck[i];

            console.log("user has edit permission: " + listToCheck.list.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems));
            console.log("view details: " + listToCheck.view.get_htmlSchemaXml());
        }
    }), Function.createDelegate(this, function()
        {
            alert("error");
        }));
}, "sp.js");
  • if all the lists and views exist then the code works
  • but if any list or view doesn't exist then the entire thing errors

I'd also like to get field details, particularly the field type, but I can't figure out how to add that to here.

2 Answers 2

1

I believe you can use SP.ExceptionHandlingScope as a 'batch try/catch' which will not cause your entire operation to fail if just one call fails on the server after you send it off.

Something like this (note that I have not personally used this function, but it is documented (https://msdn.microsoft.com/en-us/library/office/ff411960%28v=office.14%29.aspx)):

var e = new SP.ExceptionHandlingScope(clientContext);
var s = e.startScope();
for(var i = 0; i < listsToCheck.length; ++i)
{
    var listToCheck = listsToCheck[i];
    //try
    var t = e.startTry();
    listToCheck.list = clientContext.get_web().get_lists().getByTitle(listToCheck.listName);
    clientContext.load(listToCheck.list);
    clientContext.load(listToCheck.list, 'EffectiveBasePermissions');

    listToCheck.view = listToCheck.list.get_views().getByTitle(listToCheck.viewName);
    clientContext.load(listToCheck.view);

    t.dispose();

    //catch
    var c = e.startCatch();
    // Do something on the server...

    //finally
    var f = e.startFinally();
    // Finally do something on the server...
    f.dispose();

    //End Exception-Handling Scope
    s.dispose();

}

For the fields, the SchemaXml on the view should have a <viewfields> element that you can parse out. otherwise you should also be able to access the fields property of the SP.List object.

1

XMLNode abc= clientcontext.GetListandView(List Name) will give you views details in form of XML. Now you need to parse and get all the view GUID and pass it to GetListItems().

WS_Lists.Lists client = new WS_Lists.Lists();
            client.Credentials = new NetworkCredential(Slogin1.Username, Slogin1.Password);
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields =
                xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions =
                xmlDoc.CreateElement("QueryOptions");
            query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" + "<Value Type=\"Counter\">0</Value></Gt></Where>";
            viewFields.InnerXml = "<FieldRef Name=\"Title\" />" + "<FieldRef Name =\"fRecurrence\" />" + "<FieldRef Name =\"RecurrenceID\" />" + "<FieldRef Name =\"RecurrenceData\" />";               
            XmlNode listItems = client.GetListItems(ListTitle, "View Guid", query, viewFields, "0", queryOptions, null);

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.