Sharepoint File Upload
I have a Silverlight Application, that uploads file(s) to a Sharepoint Site. I got it working for a Sharepoint site in my box and in a Sharepoint site in someone else's box, where I login using my Windows account to that site. But I am not able to upload files into Sharepoint sites which are in a different domain / use a different login.
For uploading files into sites which do not use my account. I use this to get the credentials and to change the endpoints
CopySoa开发者_如何转开发pClient copy = new CopySoapClient();
copy.ClientCredentials.UserName.UserName = obj.UserName;
copy.ClientCredentials.UserName.Password = obj.Password;
copy.Endpoint.Address = new System.ServiceModel.EndpointAddress(newMaster.Url + "/_vti_bin/Copy.asmx");
copy.Endpoint.Contract.Name = "CopyReference.CopySoap";
copy.Endpoint.Binding.Name = "basicHttpBinding";
copy.Endpoint.Contract.ConfigurationName = "CopySoap";
where obj.UserName has the DomainName too.. For eg. Domain\UserName.
Here is a snippet of code that I have used in the past to upload a document to a SharePoint document library using the SharePoint web services SDK.
var copyClient = new Copy();
copyClient .Credentials = credentials; // <-- Create Network Credentials
var siteUrl = "http://....";
var libraryName = "MyDocLibrary";
var localFilePath = "...";
var fileName = Path.GetFileName(localFilePath);
var destinationUrl = string.Format("{0}/{1}/{2}", siteUrl, libraryName, fileName);
var fileBytes = File.ReadAllBytes(localFilePath);
var info = new[]{new FieldInformation
{
DisplayName = fileName,
Id = Guid.NewGuid(),
InternalName = fileName,
Type = FieldType.File,
Value = fileName
}};
CopyResult[] results;
copyClient.CopyIntoItems(destinationUrl, new[] { destinationUrl }, info, fileBytes, out results);
FYI - This Copy
proxy was generated using the old school Add Web Reference instead of the newer Add Service Reference. I find it easier to use the old school proxies when working with the old ASMX SharePoint web services.
and if you need to dynamically change the endpoint url you can use the following before invoking the CopyIntoItems()
method.
var copyServiceUrl = string.Concat(siteUrl, "/_vti_bin/Copy.asmx");
copyClient.Url = copyServiceUrl;
I got it working in a different way actually. I found the site was not really authenticated when I used a WebBrowser Control to navigate to that sharepoint document library / folder location where I wanted to upload. Since I am using Silverlight I was not able to use this,
copy.ClientCredentials.Windows.ClientCredential.Domain.
which I could have used in ASP.Net applications to upload files.
So I manually prompted the user to enter the credentials inside the WebBrowser control. Now everything is working fine. The problem was that I was not able to pass the credential across the Domain, or was not able to specify the Domain from Silverlight.
PS: I had client access policies and cross domain. So it has nothing to do with this. (as far as I know).
If I could get a solution without this workaround. It would be highly appreciated.
Thank you Wallace for your interest once again.
精彩评论