Question

As a follow up to this post, I have a document that I am downloading from OneDrive library using CSOM.

private static void DownloadFile(ClientContext context, string listTitle, int listItemId, string downloadPath)
{
    var list = context.Web.Lists.GetByTitle(listTitle);
    var listItem = list.GetItemById(listItemId);
    context.Load(listItem, i => i.File);
    context.ExecuteQuery();

    var fileRef = listItem.File.ServerRelativeUrl;
    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);
    var fileName = Path.Combine(downloadPath, listItem.File.Name);
    using (var fileStream = System.IO.File.Create(fileName))
    {
        fileInfo.Stream.CopyTo(fileStream);
    }
}

Along with the document, I also need to get the document's Major Version, Minor Version, Modified by values etc. However, any code that I have found, only gives me "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested" when I attempt to get the value from File.ListItemAllFields property.

How can I access those values?

EDIT: Based on this post The property or field has not been initialized. It has not been requested or the request has not been executed when I try

var listItemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());  

enter image description here

I can see the values in the Add Watch window. The fields in red are the ones I need to access (along with Version).

Was it helpful?

Solution 2

if this helps anyone, this code returns the values I needed.

List listFields = context.Web.Lists.GetByTitle("Financial Notes");
context.Load(listFields);
context.Load(listFields.ContentTypes);
context.Load(listFields.Fields);
ListItem item;
// this is how you can get the item(file), you can also use CAML query
context.Load(item = listFields.GetItemById(listItemId));
//here you can get the dictonary for fieldvalue
context.Load(item.FieldValuesAsText);
context.ExecuteQuery();

//CreatedBy
string sCreatedBy = item.FieldValuesAsText["Created_x0020_By"];
//ModifiedBy
string modifiedBy = item.FieldValuesAsText["Modified_x0020_By"];
//TimeCreated
string sTimeCreated = item.FieldValuesAsText["Created_x0020_Date"];
//TimeModified
string sTimeModified = item.FieldValuesAsText["Last_x0020_Modified"];
//_CheckinComment
string sCheckinComment = item.FieldValuesAsText["_CheckinComment"];
//Title
string sTitle = item.FieldValuesAsText["Title"];
//MetaData Type
string sMetaDataType = item.FieldValuesAsText["Financial_x0020_Notes_x0020_Type"];

//Microsoft.SharePoint.Client.FieldUserValue UserValue = 
string sUserValue = ((FieldUserValue)listItem["Author"]).LookupValue;
TaxonomyFieldValue taxFieldValue = item["Metadata"] as TaxonomyFieldValue;

OTHER TIPS

This error occurs since requested properties have not been loaded. You have to explicitly specify them in ClientRuntimeContext.Load method.

Since you are getting ListItemAllFields property, you have to specify like this:

context.Load(listItem, i => i.File,i => i.File.ListItemAllFields);

Example 1

In order to retrieve File object with all List Item properties you could use the following code:

var list = context.Web.Lists.GetByTitle(listTitle);
var listItem = list.GetItemById(listItemId);
context.Load(listItem, i => i.File,i => i.File.ListItemAllFields);
context.ExecuteQuery();
Console.WriteLine(listItem.File.ListItemAllFields.FieldValues["Modified_x0020_By"]);

Example 2

There is a better way to retrieve File properties. The following example demonstrates how to load File object associated with List Item and print its properties:

var list = context.Web.Lists.GetByTitle(listTitle);
var listItem = list.GetItemById(listItemId);
context.Load(listItem, i => i.File);
context.ExecuteQuery();
//print file properties
Console.WriteLine(listItem.File.ModifiedBy.LoginName);
Console.WriteLine(listItem.File.MajorVersion); 

Do you want to retrieve the properties? If yes, then you will find it within your SPListItem -> listItem["Modified_x0020_By"], etc.. But not all of them. Via listItem.File.Versions you get a collection of the file versions which you can iterate through respectively can read the properties e.g. "Level" (Draft, Published...) or "VersionLabel" (-> version number) of each file version.

Please, let me know if this is what you have wanted to know.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top