Link to home
Start Free TrialLog in
Avatar of vbtl
vbtlFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Upload openXML Doc from memorystream to a sharepoint Document Library

Hi, I am having trouble finding correct information about how to do the above.  I am using a Lightswitch Cloud App and can pass item names to the C# handler and process to manipulate the document in a stream.  I have a requirement:
Download a Document from a SharePoint 2013 online list, remove the "Template" name to create a new document in memory.  Then get data from a few other sharepoint lists through the  list data sources that I have created (StandardListDS with StandardtList items) find tags in the document, and replace the tags with data.  Then rename the file without the "template" word and save it back to the sharepoint document library.  If the document does not exist, then it should create new, if it exists, it should check-out and then update with new version and check-in.  
I have probably rather messily got to the point where I think (not been able to check yet), I have created the doc with the updated data, by parsing the template for tags, finding out what the tags were and the data I need to replace in the document and then doing a search and replace.  But now I am stuck as all the examples I have found as to how to upload the document reference SPList (which is not available) or other methods that are not available.  Any help and pointers are very much welcomed.
I am using Visual Studio 2015 Community with the Office Tools installed for the SharePoint connectivity.

UPDATE: I think that I may have figured out the upload, but when it runs I am getting a 401 unauthorised error.  I can manually upload, but through code returns the error.  
Code used:

private ClientContext _clientContext;
_clientContext = _ctx.Application.SharePoint.GetHostWebClientContext();
private MemoryStream _doc = new MemoryStream();

// save file
                    Microsoft.SharePoint.Client.Web web = _clientContext.Web;
                    _clientContext.Load(web);
                    _clientContext.ExecuteQuery();
                    string targetFileUrl = "/" + _docFolderString + "/" +  _fileName;

                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(_clientContext, targetFileUrl, _doc, true);
                    Microsoft.SharePoint.Client.File uploadedFile =  web.GetFileByServerRelativeUrl(targetFileUrl);

401 error always returns on the SaveBinaryDirect call.
ASKER CERTIFIED SOLUTION
Avatar of vbtl
vbtl
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial