ShareThis

Upload image to Picture Library using SharePoint 2010 Web Services

We can use http:///_vti_bin/Copy.asmx Web service to upload files to a SharePoint document library or a picture library from another list. Here we use "GetItem" method to download files from the source list and "CopyIntoItems" method to upload files to the destination list. But there is a limitation that you can't copy between locations that are not on the same Web Application directly.

Following is a workaround for that limitation using two references to the source and destination sites. In this way it is possible to post an image from a SharePoint server located in one domain to another SharePoint server located in a separate domain.

In here “IntranetBlogCopyService” is a Web Reference to the http://moss2010/sites/IntranetBlog/_vti_bin/Copy.asmx service and “InternetBlogCopyService” is a a Web Reference to the ‘http://prasadmoss.domainx.local/sites/InternetBlog/_vti_bin/Copy.asmx” service.


///
/// Initialize Intranet Blog Copy Service.
///

private IntranetBlogCopyService.Copy InitializeIntranetBlogCopyService()
{
IntranetBlogCopyService.Copy intranetBlogCopyService = new IntranetBlogCopyService.Copy();
intranetBlogCopyService.Credentials = CredentialCache.DefaultCredentials;
intranetBlogCopyService.Url = "http://moss2010/sites/IntranetBlog/_vti_bin/Copy.asmx";
return intranetBlogCopyService;
}


///
/// Initialize Internet Blog Copy Service.
///

private InternetBlogCopyService.Copy InitializeInternetBlogCopyService()
{
InternetBlogCopyService.Copy internetBlogCopyService = new InternetBlogCopyService.Copy();
internetBlogCopyService.Credentials = CredentialCache.DefaultCredentials;
internetBlogCopyService.Url = "http://prasadmoss.domainx.local/sites/InternetBlog/_vti_bin/Copy.asmx";
return internetBlogCopyService;
}


///
/// Copy image between two Picture Libraries.
///

private void PostAnImageFromIntranetSiteToInternetSite()
{
IntranetBlogCopyService.Copy intranetBlogCopyService = InitializeIntranetBlogCopyService();
InternetBlogCopyService.Copy internetBlogCopyService = InitializeInternetBlogCopyService();

string sourceAbsoluteUrl = "http://moss2010/sites/InternetBlog/Lists/Photos/image01.png";
string[] destinationAbsoluteUrls = { "http://prasadmoss/sites/InternetBlog/Lists/Photos/image01.png" };

IntranetBlogCopyService.FieldInformation intranetFieldInfo = new IntranetBlogCopyService.FieldInformation();
IntranetBlogCopyService.FieldInformation[] intranetFieldInfoArray = { intranetFieldInfo };
byte[] imageByteArray;
uint getUint = intranetBlogCopyService.GetItem(sourceAbsoluteUrl, out intranetFieldInfoArray, out imageByteArray);

// Create FieldInformation of InternetCopyService type.
InternetBlogCopyService.FieldInformation internetFieldInfo = new InternetBlogCopyService.FieldInformation();

// Copy individual fields.
internetFieldInfo.DisplayName = intranetFieldInfoArray[0].DisplayName;
internetFieldInfo.InternalName = intranetFieldInfoArray[0].InternalName;
internetFieldInfo.Value = intranetFieldInfoArray[0].Value;
internetFieldInfo.Id = intranetFieldInfoArray[0].Id;

InternetBlogCopyService.FieldInformation[] internetFieldInfoArray = { internetFieldInfo };

InternetBlogCopyService.CopyResult copyResult1 = new InternetBlogCopyService.CopyResult();
InternetBlogCopyService.CopyResult[] copyResultArray = { copyResult1 };

try
{
uint myCopyUint = internetBlogCopyService.CopyIntoItems(sourceAbsoluteUrl, destinationAbsoluteUrls, internetFieldInfoArray, imageByteArray, out copyResultArray);
if (myCopyUint == 0)
{
int idx = 0;
foreach (InternetBlogCopyService.CopyResult myCopyResult in copyResultArray)
{
string opString = (idx + 1).ToString();
if (copyResultArray[idx].ErrorMessage == null)
{
// Copy operation success.
}
else
{
// Copy operation failed.
}
idx++;
}
}
}
catch (Exception ex)
{
// Error
}
}

0 comments: