从信息路径库复制附件并保存到SharePoint列表 [英] Copy Attachments from Info path Library and Save to SharePoint List
问题描述
我已经开始实施以下要求。
我想读取xml文件的数据Infopath表单库并将附件存储到文件流中,然后使用SaveBinaryDirect方法将其保存到SharePoint列表。
使用以下代码满足上述要求。
(childenode ==
" Comments_Attachments" ; )
// childnode是xml文件中的节点名称
& NBSP;                         {
                      &NBSP ;
字符串 s = Innertext;
                      &NBSP ;
字节 [] b =
转换 。FromBase64String(s);
                      &NBSP ;
int nameBufferLen = b [20] * 2;
                      &NBSP ;
//文件名缓冲区以获取带扩展名的文件名
                      &NBSP ;
字节 [] fileNameBufffer =
新
字节 [nameBufferLen];
                      &NBSP ;
for ( int
i = 0; i< nameBufferLen; i ++)
< span style ="font-size:9.5pt; FONT-FAMILY:索拉;色:黑色">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
fileNameBufffer [I] = b [24 + i];
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
字符 [] charFileName =
UnicodeEncoding .Unicode.GetChars(fileNameBufffer);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; 字符串
fileName = 新
字符串 (charFileName);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
文件名= fileName.Substring(0,fileName.Length - 1 );
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
//剩余内容的字节数组
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; 字节 []
fileContent = 新
字节 [b.Length - (24 + nameBufferLen)];
< p style ="text-autospace:none"> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
for ( int
i = 0; i< fileContent.Length; i ++)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; fileContent [I] = b [24 + nameBufferLen + i];
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
字符串 filepath =
" C:\ test" ;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
FileStream fs =
新
FileStream ( @" C:\ test \" < span style ="font-size:9.5pt; font-family:Consolas; color:black">
+ fileName, FileMode 。创建);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
fs.Write(fileContent,0,fileContent.Length);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
fs.Close();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
fileext = fs.Name;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
字符串 fileexten = 路径 .GetExtension(fileext);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
使用 ( FileStream
oFileStream = new
FileStream (fileext,
FileMode 。打开, FileAccess .ReadWrite, FileShare .Re adWrite))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; 控制台 。WriteLine(fileName,
fileext,fileexten);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
< span style ="font-size:9.5pt; FONT-FAMILY:索拉;颜色:蓝色">字符串 attachmentpath =
" / Lists / ListName / Attachments /" + itemid +
" /" + fileName + "" ;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
< span style ="font-size:9.5pt; FONT-FAMILY:索拉;颜色:蓝色">字符串 siteurl = sourcesiteUrl + attachmentpath;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
< span style ="font-size:9.5pt; FONT-FAMILY:索拉;颜色:#2B91AF">控制台 .WriteLine(siteurl);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&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NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;
< p style ="text-autospace:none"> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; }
使用上述方法我能够将附件保存到我的物理路径中,并且附件URL也正在形成。但是在执行上下文时,我遇到错误。
错误:无法调用HTTP DAV请求。有待处理的查询。
如果有,请告诉我们我犯了任何错误
问候,
Sudheer
谢谢&此致,Sudheer
错误:
附加信息:无法调用HTTP DAV请求。有待处理的查询。
当 ClientRuntimeContext.HasPendingRequest
property 返回true。
如果是 File.OpenBinaryDirect
方法 您可以使用以下验证:if(ctx.HasPendingRequest)
ctx.ExecuteQuery();
示例private static void DownloadFile(Web web,string fileUrl,string targetPath)
{
var ctx =(ClientContext)web.Context;
if(ctx.HasPendingRequest)
ctx.ExecuteQuery();
。使用(VAR的fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(CTX,fileUrl))
{
变种文件名= Path.Combine( targetPath,System.IO.Path.GetFileName(fileUrl));
using(var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream) );
}
}
}
Hi,
I have the below requirement which I already started implementing.
I want to read the xml file data of Infopath form library and store the attachment into filestream and then save the same to SharePoint list using SaveBinaryDirect method.
Am using below code for above requirement.
if (childenode == "Comments_Attachments") // childnode is the node name in xml file
{
string s = Innertext;
byte[] b = Convert.FromBase64String(s);
int nameBufferLen = b[20] * 2;
// file name buffer to get the filename with extension
byte[] fileNameBufffer = new Byte[nameBufferLen];
for (int i = 0; i < nameBufferLen; i++)
{
fileNameBufffer[i] = b[24 + i];
}
char[] charFileName = UnicodeEncoding.Unicode.GetChars(fileNameBufffer);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);
// byte array for the remaining content
byte[] fileContent = new byte[b.Length - (24 + nameBufferLen)];
for (int i = 0; i < fileContent.Length; i++)
{
fileContent[i] = b[24 + nameBufferLen + i];
}
string filepath = "C:\test";
FileStream fs = new FileStream(@"C:\test\" + fileName, FileMode.Create);
fs.Write(fileContent, 0, fileContent.Length);
fs.Close();
fileext = fs.Name;
string fileexten=Path.GetExtension(fileext);
using (FileStream oFileStream = new FileStream(fileext, FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite))
{
Console.WriteLine(fileName, fileext, fileexten);
string attachmentpath = "/Lists/ListName/Attachments/" + itemid + "/" + fileName +"";
string siteurl = sourcesiteUrl + attachmentpath;
Console.WriteLine(siteurl);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, siteurl, oFileStream, true);
ctx.Load(dlist);
ctx.ExecuteQuery();
}
}
Using the above method I can able to save the attachment into my physical path and the attachment URL also is getting formed properly. But while executing the context am getting below error.
Error : Cannot invoke HTTP DAV request. There is a pending query.
Please let me know if I am making any mistake
Regards,
Sudheer
Thanks & Regards, Sudheer
The error:
Additional information: Cannot invoke HTTP DAV request. There is a pending query.
is encountered when ClientRuntimeContext.HasPendingRequest property returns true.
In case of File.OpenBinaryDirect method you could use the following verification:
if (ctx.HasPendingRequest) ctx.ExecuteQuery();
Example
private static void DownloadFile(Web web, string fileUrl,string targetPath) { var ctx = (ClientContext)web.Context; if (ctx.HasPendingRequest) ctx.ExecuteQuery(); using(var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileUrl)) { var fileName = Path.Combine(targetPath, System.IO.Path.GetFileName(fileUrl)); using (var fileStream = System.IO.File.Create(fileName)) { fileInfo.Stream.CopyTo(fileStream); } } }
这篇关于从信息路径库复制附件并保存到SharePoint列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!