属性或字段"ServerRelativeUrl"尚未初始化.尚未请求或尚未执行请求.可能需要明确要求. [英] The property or field 'ServerRelativeUrl' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
问题描述
我正在尝试将< g class ="gr_gr_109 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace"从Sharepoint在线复制到SharePoint Foundation 2013. data-gr-id ="109". id ="109" i/g. 在代码下方编写了代码,并出现错误
此代码有问题:
Microsoft.SharePoint.Client.File.SaveBinaryDirect(Foundationctx_img,< g class ="gr_ gr_181 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id ="181" id ="181"> onpremfile</g> .ServerRelativeUrl, fileInfo.Stream,true);
if(!FoundationItemColl.Any(x => x.FieldValues ["FileLeafRef"].ToString()== Onlineitem ["FileLeafRef"].ToString()))
{
FileCreationInformation objFileInfo_img =新的FileCreationInformation();
字符串newImgTitle = Onlineitem ["FileLeafRef"].ToString();
字符串FileRef = Onlineitem ["FileRef"].ToString();
objFileInfo_img.Url = newImgTitle;
//var fi = new FileInfo(newImgTitle);
//var fileUrl = String.Format("{0}/{1}",list.RootFolder.ServerRelativeUrl,fi.Name);
//objFileInfo_img.Content = System.Text.Encoding.ASCII.GetBytes(newImgTitle);
//objFileInfo_img.Content = System.Text.Encoding.ASCII.GetBytes(FileRef);
Microsoft.SharePoint.Client.File onlinefile = Onlineitem.File;
onlineCtx.Load(onlinefile);
//onlineCtx.ExecuteQuery();
ListItem foundationImageItem = onlinefile.ListItemAllFields;
//Foundationctx_img.Load(foundationImageItem);
Microsoft.SharePoint.Client.File onpremfile = foundationImageItem.File;
onlineCtx.ExecuteQuery();
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect((ClientContext)Onlineitem.Context,onlinefile.ServerRelativeUrl);
onlineCtx.ExecuteQuery();
Foundationctx_img.ExecuteQuery();
Microsoft.SharePoint.Client.File.SaveBinaryDirect(Foundationctx_img,onpremfile.ServerRelativeUrl,fileInfo.Stream,true);
//Microsoft.SharePoint.Client.File文件= list.RootFolder.Files.Add(objFileInfo_img);
//UpdateFoundationImagesProperties(foundationImageItem_L,Onlineitem);
//foundationImageItem ["FileLeafRef"] = Onlineitem ["FileLeafRef"];
//foundationImageItem.Update();
//foundationImageItem.Context.ExecuteQuery();
}
< g class =" gr_ gr_209 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id ="209". id ="209">注:</g>使用注释的代码< g class ="gr_ gr_231 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace"; data-gr-id ="231". id ="231" i/g.能够复制图像,但图像为空白.
感谢帮助-谢谢< g class =" gr_ gr_314 gr-alert gr_gramm gr_inline_cards gr_run_anim样式multiReplace" data-gr-id ="314". id ="314">您!/g
以下是代码段供您参考:
字符串siteCollectionUrl ="https://Tenantname.sharepoint.com/sites/sitename/"; 字符串userName ="xxx@xxx.onmicrosoft.com"; 字符串密码="xxxxxx"; //命名空间:Microsoft.SharePoint.Client ClientContext ctxOnline =新的ClientContext(siteCollectionUrl); //命名空间:System.Security SecureString secureString = new SecureString(); password.ToList().ForEach(secureString.AppendChar); //命名空间:Microsoft.SharePoint.Client ctxOnline.Credentials =新的SharePointOnlineCredentials(userName,secureString); //命名空间:Microsoft.SharePoint.Client 网站site = ctxOnline.Site; ctxOnline.Load(site); ctxOnline.ExecuteQuery(); 列表列表= ctxOnline.Web.Lists.GetByTitle("Documents"); ListItem项目= list.GetItemById(3); ctxOnline.Load(list); ctxOnline.Load(item,i => i.File); ctxOnline.ExecuteQuery(); var fileRef = item.File.ServerRelativeUrl; FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctxOnline,fileRef); 使用(ClientContext ctxpremise = new ClientContext("http://sp/sites/dev/")) { //假设该网站有一个名为"doc2"的库. var formLib = ctxpremise.Web.Lists.GetByTitle("doc2"); ctxpremise.Load(formLib.RootFolder); ctxpremise.ExecuteQuery(); Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctxpremise,formLib.RootFolder.ServerRelativeUrl +"/test.png",fileInfo.Stream,true); ctxpremise.ExecuteQuery(); }在File.SaveBinaryDirect方法中,如果要使用RootFolder.ServerRelativeUrl,请在本地ClientContext中加载库根文件夹对象.
这是从SharePoint Online Documents库下载Cpature.png到本地doc 2库的结果:
谢谢
最好的问候
I am trying to copy images from Sharepoint online to SharePoint Foundation 2013 for that <g class="gr_ gr_109 gr-alert gr_tiny gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling multiReplace" data-gr-id="109" id="109">i</g> have written code below code and getting error
This code having an issue :
Microsoft.SharePoint.Client.File.SaveBinaryDirect(Foundationctx_img, <g class="gr_ gr_181 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="181" id="181">onpremfile</g>.ServerRelativeUrl, fileInfo.Stream, true);
if (!FoundationItemColl.Any(x => x.FieldValues["FileLeafRef"].ToString() == Onlineitem["FileLeafRef"].ToString())){
FileCreationInformation objFileInfo_img = new FileCreationInformation();
string newImgTitle = Onlineitem["FileLeafRef"].ToString();
string FileRef = Onlineitem["FileRef"].ToString();
objFileInfo_img.Url = newImgTitle;
//var fi = new FileInfo(newImgTitle);
//var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);
// objFileInfo_img.Content = System.Text.Encoding.ASCII.GetBytes(newImgTitle);
//objFileInfo_img.Content = System.Text.Encoding.ASCII.GetBytes(FileRef);
Microsoft.SharePoint.Client.File onlinefile = Onlineitem.File;
onlineCtx.Load(onlinefile);
//onlineCtx.ExecuteQuery();
ListItem foundationImageItem = onlinefile.ListItemAllFields;
//Foundationctx_img.Load(foundationImageItem);
Microsoft.SharePoint.Client.File onpremfile = foundationImageItem.File;
onlineCtx.ExecuteQuery();
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect((ClientContext)Onlineitem.Context, onlinefile.ServerRelativeUrl);
onlineCtx.ExecuteQuery();
Foundationctx_img.ExecuteQuery();
Microsoft.SharePoint.Client.File.SaveBinaryDirect(Foundationctx_img, onpremfile.ServerRelativeUrl, fileInfo.Stream, true);
//Microsoft.SharePoint.Client.File file = list.RootFolder.Files.Add(objFileInfo_img);
//UpdateFoundationImagesProperties(foundationImageItem_L, Onlineitem);
//foundationImageItem["FileLeafRef"] = Onlineitem["FileLeafRef"];
//foundationImageItem.Update();
//foundationImageItem.Context.ExecuteQuery();
}
<g class="gr_ gr_209 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="209" id="209">Note :</g> using commented code <g class="gr_ gr_231 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="231" id="231">i</g> was able to copy images but images were blank.
Help appreciated- Thank <g class="gr_ gr_314 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="314" id="314">you !</g>
Hi,
Here is a code snippet for your reference:
string siteCollectionUrl = "https://Tenantname.sharepoint.com/sites/sitename/"; string userName = "xxx@xxx.onmicrosoft.com"; string password = "xxxxx"; // Namespace: Microsoft.SharePoint.Client ClientContext ctxOnline = new ClientContext(siteCollectionUrl); // Namespace: System.Security SecureString secureString = new SecureString(); password.ToList().ForEach(secureString.AppendChar); // Namespace: Microsoft.SharePoint.Client ctxOnline.Credentials = new SharePointOnlineCredentials(userName, secureString); // Namespace: Microsoft.SharePoint.Client Site site = ctxOnline.Site; ctxOnline.Load(site); ctxOnline.ExecuteQuery(); List list = ctxOnline.Web.Lists.GetByTitle("Documents"); ListItem item = list.GetItemById(3); ctxOnline.Load(list); ctxOnline.Load(item, i => i.File); ctxOnline.ExecuteQuery(); var fileRef = item.File.ServerRelativeUrl; FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctxOnline, fileRef); using (ClientContext ctxpremise = new ClientContext("http://sp/sites/dev/")) { // Assume that the web site has a library named "doc2". var formLib = ctxpremise.Web.Lists.GetByTitle("doc2"); ctxpremise.Load(formLib.RootFolder); ctxpremise.ExecuteQuery(); Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctxpremise, formLib.RootFolder.ServerRelativeUrl+"/test.png", fileInfo.Stream, true); ctxpremise.ExecuteQuery(); }In File.SaveBinaryDirect method, if want to use RootFolder.ServerRelativeUrl, please load library rootfolder object in On-Premise ClientContext.
Here is the result download Cpature.png from SharePoint Online Documents library to On-Premise doc 2 library:
Thanks
Best Regards
这篇关于属性或字段"ServerRelativeUrl"尚未初始化.尚未请求或尚未执行请求.可能需要明确要求.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!