我遇到了这个错误,我不明白为什么。你能帮我吗?我试图通过客户端对象模型和OpenXML编辑Sharepoint上的PowerPoint文件。编辑工作正常但由于某种原因我被抛出block length does not match with its complement
错误。我还没有在google上找到任何有帮助的东西。
以下是有问题的守则:
class Program
{
// Credentials to log into the Sharepoint.
private static NetworkCredential credential = new NetworkCredential("login", "pwd", "domain");
static private void CopyStream(Stream source, Stream destination)
{
byte[] buffer = new byte[32768];
int bytesRead;
do
{
bytesRead = source.Read(buffer, 0, buffer.Length);
destination.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}
static void Main(string[] args)
{
try
{
// Creating the Clientcontext
ClientContext clientContext = new ClientContext("https://sharepoint.adress.com/folder/");
clientContext.Credentials = credential;
Web oWebsite = clientContext.Web;
// Collect all lists from Sharepoint.
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
Console.WriteLine("Connected to Sharepoint.");
// Getting the Presentation.
Console.WriteLine("Working on " + filename + "..");
Guid id = new Guid("2BBF9030-66C6-4F71-86E6-DFE41F57B9F3"); // List ID for the Slide Library.
List sharedDocumentsList = clientContext.Web.Lists.GetById(id);
CamlQuery camlQuery = new CamlQuery();
String queryString = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + filename +
"</Value></Eq></Where><RowLimit>1</RowLimit></Query></View>"; // FileLeafRef is the column "name" on the Sharepoint.
camlQuery.ViewXml = @queryString;
ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
clientContext.Credentials = credential;
clientContext.Load(sharedDocumentsList);
clientContext.ExecuteQuery();
clientContext.Credentials = credential;
clientContext.Load(listItems);
clientContext.ExecuteQuery();
Console.WriteLine("Opened Document.");
if (listItems.Count == 1)
{
ListItem item = listItems[0];
clientContext.Credentials = credential;
FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
using (MemoryStream memoryStream = new MemoryStream())
{
CopyStream(fileInformation.Stream, memoryStream);
using (PresentationDocument ppt = PresentationDocument.Open(memoryStream, true))
{
string rId = GetSlidePart(ppt, slideNumber - 1);
PresentationPart pptPart = ppt.PresentationPart;
SlidePart slide = (SlidePart)pptPart.GetPartById(rId);
Boolean exists = false;
//groupExists(slide.Slide, "IDGroup", exists);
if (!groupExists(slide.Slide, "IDGroup", exists))
{
Console.WriteLine(exists);
Console.WriteLine("IDGroup not found in " + filename + ", adding it..");
EditPresentation(memoryStream, clientContext, item);
}
else
{
Console.WriteLine(exists);
Console.WriteLine("IDGroup already exists, skipping Presentation..");
}
} // Error gets thrown here.
}
}
else
{
Console.WriteLine("Document not found.");
}
Console.WriteLine("Done.");
Console.Read();
}
catch (Exception ex) when (ex is ServerException || ex is InvalidCastException || ex is OpenXmlPackageException || ex is WebException)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
}
这是编辑文件的方法。修剪到我使用memoryStream的部分,可以查看完整的代码on pastebin:
public static void EditPresentation(MemoryStream memoryStream, ClientContext clientContext, ListItem item)
{
using (PresentationDocument ppt = PresentationDocument.Open(memoryStream, true))
{
string rId = GetSlidePart(ppt, slideNumber - 1);
PresentationPart pptPart = ppt.PresentationPart;
SlidePart slide = (SlidePart)pptPart.GetPartById(rId);
ShapeTree tree = new ShapeTree();
CommonSlideData commonSlideData1 = new CommonSlideData();
// 250 Lines of OpenXML go here.
slide.AddHyperlinkRelationship(new System.Uri(adress, System.UriKind.Absolute), true, "rId99");
slide.Slide.CommonSlideData.ShapeTree.Append(groupShape1);
slide.Slide.Save();
pptPart.Presentation.Save();
}
memoryStream.Seek(0, SeekOrigin.Begin);
ClientOM.File.SaveBinaryDirect(clientContext, (string)item["FileRef"], memoryStream, true);
}
有什么根本我做错了吗?我从来没有做过这样的事情,因为所有的改进建议都会受到赞赏。