SaveBinaryDirectは、クライアントオブジェクトモデルが正常に動作するときにUnauthorizedをスローします
-
10-12-2019 - |
質問
クライアントオブジェクトモデルに要求/ファイルサイズの制限がないため、SaveBinaryDirectメソッドを使用してドキュメントをsharepointサイトにアップロードすることに
アプリケーションは2つのことを行い、(クライアントオブジェクトモデルを使用して)フォルダーを追加し、選択したファイルを作成したばかりのフォル
これはここで私の設定で正常に動作します。しかし、私たちのクライアントのサイトでは、フォルダは正常に作成されますが、ファイルのアップロード部分は「Unauthorized401」エラーを返します。
SP.ClientContext ctx = new SP.ClientContext(siteurl);
SP.Web currentWeb = ctx.Web;
//Pass user details from config (That has owner permissions)
ctx.Credentials = new NetworkCredential(username, password, domain);
//create folder method (checks if the folder exists and creates using Client Object Model)
CreateFolder(ctx,libraryUrl,folderName)
//Save Binary Direct Method
string fileUrl = "/"+ libraryname + "/" + folderName + "/" + fileName;
using (data)
{
//upload by passing the context, file url, file data stream,
//set overwrite to true
SP.File.SaveBinaryDirect(ctx, fileUrl,data, true);
}
これは私に2つのことのうちの1つを考えさせます
- 承認/認証に関して、共有ポイントまたはwebサーバー上で変更する必要がある設定があります。
- ネットワーク資格情報を適用する方法が正しくありません。クライアントオブジェクトモデルに対してのみ実行されるため、save binary directは実行されません。
私はクライアントオブジェクトモデルを使用できることを認識しており、設定/設定の問題ではない場合は別のリリースを行う必要があるかもしれ
これをより多くの経験を持つ人は、なぜこれをやっているのかについていくつかのアイデアを持っていますか?
解決
SharePoint 2013を使用している場合SaveBinaryDirectを使用できないため、クレーム認証では機能しません。SaveBinaryDirectによって行われたコールには、必要な許可Cookieが含まれていません。詳細はこのブログ記事へのコメントで説明されています。ASPX "REL=" NOFOLLOW "> Office 365とSharePoint Onlineへのアクティブな認証方法
アップデート(2016年7月2日):
James Loveによるコメントを見た後、私はこれを見ました。何が変わったのかわからないが、SaveBinaryDirectがSharePoint 2013とSharePoint Onlineを使用して動作していることを示します。
これは私が使用したコードです:
.// var context = new ClientContext("http://intranet.wingtip.com/sites/demo/");
// context.Credentials = System.Net.CredentialCache.DefaultCredentials;
var context = new ClientContext("https://robwindsor2.sharepoint.com/sites/demo/");
context.Credentials = new SharePointOnlineCredentials(loginName, securePassword);
var web = context.Web;
var lib = web.Lists.GetByTitle("Documents");
context.Load(lib);
context.Load(lib.RootFolder);
context.ExecuteQuery();
var fileName = "Test.docx";
var filePath = @"C:\Users\robwindsor\Desktop\" + fileName;
using (var fs = new FileStream(filePath, FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, lib.RootFolder.ServerRelativeUrl + "/" + fileName, fs, true);
}
var query = new CamlQuery();
query.ViewXml = "<View></View>";
var files = lib.GetItems(query);
context.Load(files, c => c.Include(f => f.File));
context.ExecuteQuery();
foreach(ListItem item in files)
{
Console.WriteLine(item.File.Name);
}
Console.WriteLine("Done");
他のヒント
これを試してみてください:
FileCreationInformation fci = new FileCreationInformation();
fci.Content = newFile.Bytes;
fci.Url = sourceFile.Url;
fci.Overwrite = true;
Microsoft.SharePoint.Client.File replacedFile = list.RootFolder.Files.Add(fci);
context.Load(replacedFile);
context.ExecuteQuery();
. クライアントオブジェクトモデルを使用してファイルシステムからSharePoint 2010にファイルをアップロードすることができます。
あなたが不正な401を取得している場合は、ターゲットSharePoint Document Libraryフォルダの正しい認証情報と正しい相対URLを提供していることを確認してください。
ClientContext ctx = new ClientContext(Siteurl);
ctx.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
var sourceFilePath = @"C:\test\test.xlsx";
var targetUrl = "/Shared%20Documents";
var targetFileUrl = String.Format("{0}/{1}", targetUrl, Path.GetFileName(sourceFilePath));
var fs = new FileStream(sourceFilePath, FileMode.Open);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, targetFileUrl, fs, true);
var uploadedFile = ctx.Web.GetFileByServerRelativeUrl(targetFileUrl);
var listItem = uploadedFile.ListItemAllFields;
listItem.Update();
ctx.ExecuteQuery();
. これを追加する必要があると思います:
ctx.RequestTimeout = 3600000;
. 私は同じ問題を遭っていき、合法的なかどうかわからないが回避策を見つけました。
System.Net.WebClient webClient = new System.Net.WebClient();
System.Uri uri = new Uri(url); //Create a uri object with the file name but without extension
webClient.Credentials = new NetworkCredential(username,password,domain); //Network credentials to connect with the sharepoint server
webClient.UploadFile(uri, "PUT", pFilePath); //Upload File will push the file to the server
. TLDR:あなたが使用する必要があります サーバー コンテキストにサイト情報が含まれていても、相対URL。
私はこれと同じ問題を抱えていて、Fiddlerを使って解決しました。クライアントオブジェクトモデルを使用したとき、サイトパスを使用してコンテキストを作成しました。"https://server/sites/siteName"、その後
var docs = web.Lists.GetByTitle(documentLibraryName);
SP.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
これにより、次の投稿が行われました。 https://<server>/sites/<siteName>/_vti_bin/client.svc/ProcessQuery
パスとファイル名(およびコンテンツ)は、CSOMによってXMLに埋め込まれました。
今、 SP.File.SaveBinaryDirect
署名は SaveBinaryDirect(ClientContext context, string serverRelativeUrl, Stream stream, bool overwriteIfExists)
--しかし、要求 SaveBinaryDirect(context, "/<library>/<folder>/filename", stream, true)
401エラーで失敗しました。Fiddlerを見て、ここで行われた要求があります:
PUT https://<server>/<library>/<filename>
たとえ文脈が 含まれるもの: サイトは、あなたはそれを与える必要があります 全体のパス サーバールートから。成功した呼び出しは次のとおりです
SaveBinaryDirect(context, "/sites/<sitename>/<library>/<folder>/filename", stream, true)
そして、それは正しい場所に置くことになります。パラメータ は と呼ばれる serverRelativeUrl
結局のところ。