Goal: Download documents from a Sharepoint 2010 Document Library using C#
I've searched and found many questions/resources regarding how to download documents to a local disk from a Sharepoint Document Library. I've so far been unable to successfully implement a solution.
For the sake of discussion, my document library is located at the following link:
http://server.domain.com/sites/CompanyLocation/dprtmnts/DepartmentName/MyLibraryName/Forms/AllItems.aspx
Within this document library are two folders, each containing a number of documents. I desire to retrieve a list of these documents (in each folder) and have the ability to download/open a document on command.
Things I've tried:
The linked post from this comment: https://sharepoint.stackexchange.com/a/105923
The linked post (https://stackoverflow.com/a/21056425/2480558) offers a solution for both downloading and uploading. I only need to download, so I tried that. This solution gives me a Microsoft.SharePoint.Client.ServerObjectNullReferenceException. There are no instructions on what the url, listTitle, or listItemId need to be, so I'm probably doing something wrong... it doesn't appear to be getting any files.
I also tried the first blog post on that answer that points to using a package from bendsoft. Apparently that package is a paid package, and it's quite expensive.. I can't go that route.
Another answer: https://stackoverflow.com/a/53733630/2480558
This gives me the following exception: (actual server edited out of exception)
Microsoft.SharePoint.Client.ClientRequestException: 'The IDCRL response header from server 'http://server.domain.com/' is not valid. The response header value is 'NTLM'. The response status code is 'Unauthorized'.
This solution: https://stackoverflow.com/a/15602003/2480558
Honestly, I don't even remember why that one didn't work.
If anyone has something that might help me get on the right path... I'm not familiar with making web requests and web authorization, or anything of that nature.
If you use the example that you found at https://stackoverflow.com/a/53733630/2480558, but you have to adapt it for your local SharePoint server, since that code is logging in to SharePoint Online.
In the public void Connect() definition, replace the line
clientContext.Credentials = new SharePointOnlineCredentials(UserName, securePassword);
with this instead:
clientContext.Credentials = new NetworkCredential(UserName, securePassword, "putYourADDomainNameHere");
Related
I have been tasked with finding a way to save files given a link to someones amazon bucket. These files are attachments uploaded to a ticketing system, and when i used their API to pull the ticket out, i am given a link like the one below(I removed all the private stuff and replaced with XXXXX)
https://s3.amazonaws.com/cdn.XXXXXXX.com/data/helpdesk/attachments/production/36012348362/original/1.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXFus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180808T205357Z&X-Amz-Expires=86400&X-Amz-Signature=XXXXXXXXXXXXXX-Amz-SignedHeaders=Host
As you can see, the links time out after 1 day. I am connecting 2 API's together, and i need to be able to upload an actual file into the second API instead of just placing a link that times out after a day. I have tried to use Webclient downloadfileAsync, but all i get is a blank file output. I can go to the link in my browser and do a right click, save image as, so i know its possible, but i havent had any luck programatically. Below is the code snipped i am currently using to try and download it.
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new System.Uri(url), "image.jpg");
}
Any insight or help would be awesome.
Thanks in advance!
I was able to get this working by not using the async version of DownloadFile. Thanks to all the people who helped me resolve this.
I am trying to use sharepoint webservice to rename a folder. I tried the below code to create a folder which works fine. But I am unable to find the webmethod related to renaming folder. Any help would be highly appreciated.
Web Service used
http://myservicelink:100/_vti_bin/Dws.asmx
Dws objDws = new Dws();
objDws.Credentials = System.Net.CredentialCache.DefaultCredentials;
string strResult = objDws.CreateFolder("MyMainFolders/MyFolder");
Console.WriteLine(strResult);
Console.ReadLine();
AFAIK there is no way to do it with web services API. But do you really have reasons to use the deprecated web-services API? (one I may think of is, you need to support SharePoint 2007 and below).
The better SharePoint API is called Share Point Client Object Model (CSOM). Here is a blog article explaining basic folder operations:
Manage (Create/Delete/Rename) list folders with SP Client Object Model
I'm working on a continuing API project. The current issue at hand is to be able to download my data from the AtTask server in precisely the folder structure they exist in on the AtTask servers. I've got the folder creation working nicely; the data types between Document, Document Folder and Document Version seem to be pretty clear. I am a little disillusioned about the fact that extension isn't in the document object (that I have to refer to the document VERSION for that)... but I can see some of the reason for that from a design perspective.
The issue I'm running into now is that I need to get the file content. I originally through from the API documentation that I'd be able to get to the file contents the same way as the documentation recommends uploading it -- through the handle. Unfortunately, neither document nor docv seem to support me accessing the handle except to write a new file.
So that leaves me the "download URL" as the remaining option. If I build the UI strings from the API calls using my browser, I get a URL with https://attaskURL/document/download?ID=xxxx (and can also get the versionID and such). If I paste the url into the browser where I'm logged in to the user interface of AtTask, it works fine and I can download the file. If, instead, I use my C# code to do so, I get the login page returned as a stream for me to download instead of my actual file because I'm not authenicated. I've tried creating a network credential and attaching it to the request with the username and password, but to no avail.
I imagine there's a couple ways to solve this problem -- the easy one being finding a way to "log in" to the download site through code (which doesn't seem to be the usual network credential object in C#) OR find a way to access the file contents through the API.
Appreciate your thoughts!
It looks like you can use the download URL if you put a session id in the URL. The details on getting a session id are here (basically just call login and a session id is returned in JSON):
http://developers.attask.com/api-docs/#Authentication
Then cram it on the end of your document download URL:
https://yourcompany.attask-ondemand.com/document/download?ID=xxxx&sessionID=abc1234
I've given this a quick test and I'm able to access a document.
You can use the downloadURL and a sessionID IF you are not using SAML authentication.
I have tried it both ways and using SAML will redirect you to the login page.
I am writing a c# agent to pull data out of Excel workbooks stored in Livelink but I am unable to download the files.
webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
webClient.DownloadFile(strFileLocation, TEMP_FILE_LOC);
This is the code I am using to save the file. strFileLocation is a link generated using the make link utility in Livelink and it is a http link
Here is the code
(http://LivelinkServer.com/Livelink/livelink.exe?func=ll.GetTZ&NextURL=%2FLivelink%2Flivelink%2Eexe%3Ffunc%3Dll%26objId%3D128027626%26objAction%3Ddownload%26viewType%3D1)
I get an excel document that has a progress bar that says "Please wait a moment while Livelink is loading..." this looks, to me, like the page that redirects the request in order to get the actual file.
I appears to use a double hop in order to direct you to the file on the Livelink server via the URL it gives you, but I do not know what webservice it calls, examining the response headers gives no clues.
Does anyone know how to correctly access a file on a Livelink sever?
Any help would be greatly appreciated, thank you.
You have to call the Web Service function (SOAP)
GetVersionContents(ID, versionNum)
to get the actual data from which you can create a local file.
Thankyou for your answers but after contacting my companies livelink department, I found that I need to access this service on a seprate port to allow the correct type of authentication
The general problem: I have some code that needs a URL to a PDF file. It seems to work for URLs I find online, but not the ones I create myself.
For example, when I use a random URL from Xamarin it works fine, but when I try to generate a URL from either DropBox or Amazon Cloud Drive it does not work.
Example URLs:
These links open harmless PDF files. Please try it:
Xamarin (works fine)
DropBox (does not work)
Amazon Cloud Drive (does not work)
As you see, in a browser (I have used Chrome to test) you will get the PDF documents to open, but not without some kind of context (except for the Xamarin one).
The code: I am developing in MonoTouch and I am using a component called mTouch PDF Reader. The code is simply:
var documentViewController = new DocumentViewController (1, "Some name here", "http://someurlhere.pdf");
ActivateController (documentViewController);
This opens a nice PDF reader inside my app, but, as I can't use my own created URLs this does not help me. This is a 3rd party library so I can't look at the code. By the way, when I use one of my URLs, the code crashes with a System.NullReferenceException with this stacktrace:
MonoTouch.Foundation.NSArray.FromNativeObjects (items={MonoTouch.UIKit.UIViewController[1]}, count=1) in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSArray.cs:109
MonoTouch.Foundation.NSArray.FromNativeObjects (items={MonoTouch.UIKit.UIViewController[1]}) in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSArray.cs:96
MonoTouch.Foundation.NSArray.FromNSObjects (items={MonoTouch.UIKit.UIViewController[1]}) in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSArray.cs:48
MonoTouch.UIKit.UIPageViewController.SetViewControllers (viewControllers={MonoTouch.UIKit.UIViewController[1]}, direction=MonoTouch.UIKit.UIPageViewControllerNavigationDirection.Forward, animated=false, completionHandler={MonoTouch.UIKit.UICompletionHandler}) in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIPageViewController.g.cs:144
mTouchPDFReader.Library.Views.Core.DocumentViewController.ViewDidLoad () in
MonoTouch.UIKit.UIApplication.UIApplicationMain () in
MonoTouch.UIKit.UIApplication.Main (args={string[0]}, principalClassName=(null), delegateClassName="AppDelegate") in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
Exam936.Application.Main (args={string[0]}) in /Users/EdGriMac/Dropbox/Quiz/Code/Exam926/Exam936/Main.cs:16
The frustration:
Is there a specific way to create URLs that work in this way? It does seem like DropBox does something different as it sort of iFrames the document or something. I don't know what Amazon Cloud Drive does. What has Xamarin done? Is it, as pointed out in the comments, because of http vs https?
I am completely lost. Am I missing something simple? Do you have any other way to create URLs to suggest? Googling this is really difficult as I continue to hit examples of how to share a URL in DropBox and so on...
By the way, I do not want to have the documents as part of the app as this means I will have to create a new version of the app just to change something in a document.
Update 1: I have added links above. I will try some other suggestions later and will leave more updates. Thanks in advance for any further suggestions!
Update 2: I have used Fiddler to look at the response on each of the URLs. The Xamarin URL has Content-Type: application/pdf while both DropBox and Amazon Cloud Drive has Content-Type: text/html; charset=UTF-8. This explains a lot. I will try andersr's suggestion later today as I do have a web server to put files on.
Update 3 When I put the PDF file on my Amazon EC2 server, created a virtual directory under my web site in IIS, the URL to my website + virtual directory + filename worked! Turns out the Content-Type had to be application/pdf for the mTouch PDF Reader to open it through a URL.
Thanks everyone for your help!
It seems to me that the first two URLS, link directly to the PDF files, but the latter one, ie. the one on Amazon Cloud Drive links to a page which again links to the PDF. I suggest the following potential solutions:
Find a reliable way to extract the direct url to the document on cloud drive. The link to the document is not the one you provided, but this: link . Perhaps Amazon has documentation on how you can avoid the html interface in order to retrive your file. I am not familiar with cloud drive at all. Note that the url provided has some time limited token attached to it.
Host the document on infrastructure you have more control over. IE. setup your own web server and host the documents there. Alternatively use another cloud storage provider which gives you the ability to link to files directly.