0

I want to create a web part where:

based on a given document ID, I need to get the corresponding filename of the document.

So this is my function to get the document and my question is how can I modify it to get the name instead of the whole document?

private void GetDocument(string UNIQUE_DOC_ID)
{
 string siteUrl= "...";
 ClientContext clientContext = new ClientContext(siteUrl);
 List documentsList = clientContext.Web.Lists.GetByTitle("Documents"); 
 CamlQuery camlQueryDocs = new CamlQuery();
 camlQueryDocs.ViewXml =
  @"<View>
   <Query>
     <Where>
    <Eq>
      <FieldRef Name='ID' /> // Not sure about the field name. It can also be _dlc_DocId
      <Value Type='Text'>" + UNIQUE_DOC_ID + @"</Value>
    </Eq>
     </Where>
   </Query>
    </View>";
 document = documentsList.GetItems(camlQueryDocs);
 
    clientContext.Load(document);
 clientContext.ExecuteQuery();
}​
1

you can specify the column name as "FileLeafRef" in viewfield to get only specified details of document

private void GetDocument(string UNIQUE_DOC_ID)
{
string siteUrl= "...";
ClientContext clientContext = new ClientContext(siteUrl);
List documentsList = clientContext.Web.Lists.GetByTitle("Documents"); 
CamlQuery camlQueryDocs = new CamlQuery();
camlQueryDocs.ViewXml =
@"<View>
 <Query>
 <Where>
<Eq>
  <FieldRef Name='ID' /> // Not sure about the field name. It can also be _dlc_DocId
  <Value Type='Text'>" + UNIQUE_DOC_ID + @"</Value>
</Eq>
 </Where>
</Query>
<ViewFields>
<FieldRef Name='FileLeafRef' />
</ViewFields>
</View>";
document = documentsList.GetItems(camlQueryDocs);

clientContext.Load(document);
clientContext.ExecuteQuery();
}​

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.