I have a SharePoint 2010 environment that has a SSRS report. The user clicks a button and sees the report rendered via PDF. I would like to instead of downloading that PDF, place it into a document library. Is this possible via JavaScript and web services?
2 Answers
Nathan,
If you are using code behind, you can write a function to upload the generated file (must be saved to your server hard drive first) to your document library.
Here I'm calling my UploadFile function and sending it the source file destination and library destination.
UploadFile("C:\Templates\" + Num.Trim() + ".docx", "https://mysite/libraryname/" + employer + "_" + Num.Trim() + ".docx");
private void UploadFile(string srcUrl, string destUrl)
{
if (!File.Exists(srcUrl))
{
throw new ArgumentException(String.Format("{0} does not exist", srcUrl), "srcUrl");
}
SPWeb site = new SPSite(destUrl).OpenWeb();
site.AllowUnsafeUpdates = true;
FileStream fStream = File.OpenRead(srcUrl);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
EnsureParentFolder(site, destUrl);
site.Files.Add(destUrl, contents, true);
}
private string EnsureParentFolder(SPWeb parentSite, string destinUrl)
{
destinUrl = parentSite.GetFile(destinUrl).Url;
int index = destinUrl.LastIndexOf("/");
string parentFolderUrl = string.Empty;
if (index > -1)
{
parentFolderUrl = destinUrl.Substring(0, index);
SPFolder parentFolder
= parentSite.GetFolder(parentFolderUrl);
if (!parentFolder.Exists)
{
SPFolder currentFolder = parentSite.RootFolder;
foreach (string folder in parentFolderUrl.Split('/'))
{
currentFolder
= currentFolder.SubFolders.Add(folder);
}
}
}
return parentFolderUrl;
}
You can create an ISAPI web service, call the endpoint using jQuery AJAX and utilize server side code to upload to the proper location.
ISAPI Service https://msdn.microsoft.com/en-us/library/office/ms464040%28v=office.14%29.aspx
Upload file SSOM https://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint