SharePoint Client Object Model Upload Metadata and Check In File

When you enable versioning on a document library and want to use client object model upload method (in my case SaveBinaryDirect), you will find that your new uploaded file is not available in the view straightaway.

It resides in List Settings --> Manage files which have no checked in version (under Permissions and Management section) and you have to take ownership manually.

If you want to check in the file automatically (along with filling out metadata) you would need to do extra steps:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, memoryStream, true);
 
//Update metadata and check in the file
Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
 
//Checkout if checked in
clientContext.Load(file);
clientContext.ExecuteQuery();
 
if (file.CheckOutType == CheckOutType.None)
{
    file.CheckOut();
}
 
ListItem lstItem = file.ListItemAllFields;
clientContext.Load(lstItem);
clientContext.ExecuteQuery();
 
ListItem lstItem = file.ListItemAllFields;
clientContext.Load(lstItem);
clientContext.ExecuteQuery();
 
lstItem["Field Internal Name"] = "Value";
lstItem.Update();
 
clientContext.Load(file);
clientContext.ExecuteQuery();
if (file.MinorVersion == 1 || Path.GetExtension(file.Name) == ".docx")
{
    file.CheckIn("Uploaded through portal", CheckinType.MinorCheckIn);
}
else
{
    //Overwrite to avoid creating new version
    file.CheckIn("Uploaded through portal", CheckinType.OverwriteCheckIn);
}
clientContext.ExecuteQuery();
NOTE: I found that with .docx extension, even though the SaveBinaryDirect increments the minor version (debugging), when calling OverwriteCheckIn it does not increment the file minor version. Not sure if this is a bug :)

You must also fill out all required fields before being able to check the file in.

HTH,
Andreas

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

CRM Plugin - Parent and Child Pipeline