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?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityUse 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);
}
}
}
ViewFieldCollection vfc = view.ViewFields; for (int i = 0; i < vfc.Count; i++) { Console.WriteLine("field: " + vfc[i] + " position: " + (i + 1)); }
instead of using SchemaXml
.
Jun 13, 2016 at 10:42
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.