使用元数据上载文件并使用客户端对象模型签入sharepoint文件夹

时间:2013-08-16 01:26:57

标签: sharepoint-2010 sharepoint-clientobject

您好我正在尝试使用带有元数据的客户端API将文件上传到sharepoint 2010,并在完成后检查文件。以下是我的代码:

  

public void UploadDocument(SharePointFolder文件夹,字符串文件名,布尔覆盖)   {

var fileInfo = new FileInfo(filename);
var targetLocation = String.Format("{0}{1}{2}", folder.ServerRelativeUrl,
    Path.AltDirectorySeparatorChar, fileInfo.Name);

using (var fs = new FileStream(filename, FileMode.Open))
{
    SPFile.SaveBinaryDirect(mClientContext, targetLocation, fs, overwrite);
}

// doesn't work
SPFile newFile = mRootWeb.GetFileByServerRelativeUrl(targetLocation);
mClientContext.Load(newFile);
mClientContext.ExecuteQuery();

//check out to make sure not to create multiple versions
newFile.CheckOut();

// use OverwriteCheckIn type to make sure not to create multiple versions 
newFile.CheckIn("test", CheckinType.OverwriteCheckIn);
mClientContext.Load(newFile);
mClientContext.ExecuteQuery();

//SPFile uploadFile = mRootWeb.GetFileByServerRelativeUrl(targetLocation);

//uploadFile.CheckOut();
//uploadFile.CheckIn("SOME VERSION COMMENT I'D LIKE TO ADD", CheckinType.OverwriteCheckIn);

//mClientContext.ExecuteQuery();

}

我可以上传文件,但我无法添加任何元数据和文件已签出。我想添加一些元数据并在完成后检查文件。

我的SharePointFolder类具有要上载到的文件夹路径的serverRelativeUrl。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在executeQuery()之前需要一个凭证;和SaveBinaryDirect();

例如:

mClientContext.Credentials = new NetworkCredential("LoginID","LoginPW", "LoginDomain");

SPFile newFile = mRootWeb.GetFileByServerRelativeUrl(targetLocation);
mClientContext.Load(newFile);
mClientContext.ExecuteQuery();