views:

48

answers:

2

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

CopySoapClient 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.

+1  A: 

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;
Wallace Breza
@Wallace.. Thanks for responding. I am using it in a Silverlight Application. So I will need to do it asynchronously and I will need to set value for ClientCredentials. I dont think I can do it this way. Even though I do it this way using a WebService, by setting NetworkCredentials and doing it synchronously I doubt it will run on a site in a different machine, as I am implementing it using a WebService.
Aswin Ramakrishnan
A: 

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.

Aswin Ramakrishnan