0

I want to retrieve 1 column for 1 specific file using CSOM, these can be located anywhere, so in whatever folder kind of content type including document sets.

So... I could check first, then... if the result = 0, then traverse folders and see if one of the parents in line is a document set... then do a further check but I wonder if there is not a handier way to query for "both in lib and inside content types such as document sets as long as it is a file"

The following code works but.... will not work if the file is inside a document set.

                   var baseUrl = uri.GetLeftPart(UriPartial.Authority);
                    var fileServerRelativeUrl = uri.ToString().Replace(baseUrl, string.Empty);
                    logger.Log(LogLevel.Debug, " fileserverrelativeurl is " + fileServerRelativeUrl.ToString());
                    var file = context.Web.GetFileByServerRelativeUrl(fileServerRelativeUrl);                   

                    List list = file.ListItemAllFields.ParentList;
                    context.Load(list);
                    context.ExecuteQuery();
                    logger.Log(LogLevel.Debug, " list is loaded: " + list.Title.ToString());

                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml =
                        "<View><Query><Where><Eq><FieldRef Name='" + fieldRefName + "'/>" +
                        "<Value Type='Text'>" + fieldRefValue + "</Value></Eq></Where>" +
                        "<RowLimit>1</RowLimit></Query></View>";
                    logger.Log(LogLevel.Debug, " caml is " + camlQuery.ViewXml.ToString());

                    ListItemCollection listItems = list.GetItems(camlQuery);
                    context.Load(listItems);
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch
                    {
                        logger.Log(LogLevel.Debug, " caml exec FAILED !! ");
                        // e.g. : no access or the listname as incorrectly deduced
                        throw;
                    }
                    logger.Log(LogLevel.Debug, " items found: " + listItems.Count.ToString());
                    // and now retrieve the items needed
                    if (listItems.Count == 1)
                    {
                        logger.Log(LogLevel.Debug, " listitem found ");
                        ListItem item = listItems[0];
                        foreach (string column in columns)
                        {
                            if (item.FieldValues.ContainsKey(column))
                            {
                                logger.Log(LogLevel.Debug, " column found: " + column);

                                if (item[column] is Dictionary<string, object>)
                                {
                                    Dictionary<string, object> clientTaxonomyObject = item[column] as
                                        Dictionary<string, object>;
                                    if (clientTaxonomyObject.ContainsKey("_ObjectType_") &&
                                        clientTaxonomyObject.ContainsKey("TermGuid") &&
                                        clientTaxonomyObject.ContainsKey("Label") &&
                                        clientTaxonomyObject["_ObjectType_"].Equals("SP.Taxonomy.TaxonomyFieldValue"))
                                    {
                                        values.Add(column, clientTaxonomyObject["Label"].ToString());
                                    }
                                    else
                                    {
                                        values.Add(column, item[column].ToString());
                                    }

                                }
                                else
                                {
                                    values.Add(column, "");
                                }
                            }
                        }
                    }
                }

ref

UPDATE

Ah! It was much easier did not need the caml query at all just file.listitemallfields and then just dumped the taxonomy label of the field. Solved :)

Community
  • 1
  • 1
edelwater
  • 2,650
  • 8
  • 39
  • 67

1 Answers1

0

Instead of using a CAML query, it can be done much easier with

   listitem li = file.listitemallfields;
   context.load(li)
   context.ExecuteQuery();

That gives directly e.g. li["whatever"]

edelwater
  • 2,650
  • 8
  • 39
  • 67