5

In my SharePoint list, I have modified the view like this.

I want to get the 'Display' value and 'Position from Left' value for the fields. Is is possible with CSOM or another way?

3

Use the below code, it will give you the list of Fields that are in Display in the View and the postiion from left.

public static void GetViewDetails()
        {
            ClientContext context = new ClientContext("https://testsite/test");
            List list = context.Web.Lists.GetByTitle("ListName");
            context.Load(list);
            context.ExecuteQuery();
            View view = list.Views.GetByTitle("ViewName");

            context.Load(view, v=>v.ViewFields);
            context.ExecuteQuery();

            string fields = view.ViewFields.SchemaXml;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<FieldData>"+fields+"</FieldData>");
            XmlNodeList dataNodes = xmlDoc.SelectNodes("//FieldData");
            //Loading the data to the Dictionary in Position from left,Field Name format
            Dictionary<int,string> data=new Dictionary<int,string>();
            int order=0;
            foreach (XmlNode node in dataNodes)
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    order++;
                    data.Add(order, childNode.Attributes[0].Value);
                }
            }
        }
2
  • Thanks a lot ji. I just used ViewFieldCollection vfc = view.ViewFields; for (int i = 0; i < vfc.Count; i++) { Console.WriteLine("field: " + vfc[i] + " position: " + (i + 1)); } instead of using SchemaXml. – Shen Prabu Jun 13 '16 at 10:42
  • But I have one difficulty in this. I couldn't get the correct name of the field. As I analyzed in this, the view contains only 'StaticName' of the field but what exactly required is 'DisplayName'. Still wondering how to get that. – Shen Prabu Jun 13 '16 at 11:12
1

Another Way

You can get it using REST API. Just send a GET request to the following end point

/_api/Web/Lists/GetByTitle('{list name}')/Views/GetByTitle('{view name }')/ViewFields

In the response, you should get response like following

{
    "d": {
        "__metadata": {
            "id": "Web/Lists/GetByTitle('Customers')/Views/GetByTitle('All Items')/ViewFields",
            "uri": "http://xxx.sharepoint.com/_api/Web/Lists/GetByTitle('Customers')/Views/GetByTitle('All%20Items')/ViewFields",
            "type": "SP.ViewFieldCollection"
        },
        "SchemaXml": "<FieldRef Name=\"LinkTitle\" /><FieldRef Name=\"Multiusers\" /><FieldRef Name=\"ID\" />",
        "Items": {
            "__metadata": {
                "type": "Collection(Edm.String)"
            },
            "results": [
                "LinkTitle",
                "Multiusers",
                "ID"
            ]
        }
    }
}

d.Items.results array contains your field names. Array index is your position.

PS: JavaScript array position starts from 0. So the actual position will be item index plus one.

1
  • Thanks a lot. I haven't tested your code because I am using CSOM only. But seems to be a great way I will try that. – Shen Prabu Jun 13 '16 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.