Archive for March, 2010

03/16/10
Fahad Zia

Uploading files to a picture library using SharePoint Copy.asmx Web Service


This function uploads all files in folder called “ProfilePics” to a site collection picture library called “Profile”

private static void ImportPics()
{
Console.WriteLine("Absolute URL of site collection under which Profile picture library exists:");
strURL = Console.ReadLine();           

string strProfileLibName = "Profile";
string strPictureFolder = "ProfilePics"; //folder name of pics which resides at same level as this exe
string strAccountName = "";
DirectoryInfo diProfile = new DirectoryInfo(strPictureFolder);
byte[] bytearrContent;
string strDestinationUrl = "";
string strSQL="";
StringBuilder sbSQL = new StringBuilder();
spwebsvcCopy.Copy wsCopy = new ProfilePicUpdater.spwebsvcCopy.Copy();
wsCopy.Url = string.Format("{0}/_vti_bin/copy.asmx", strURL.TrimEnd('/'));
wsCopy.UseDefaultCredentials = true;

foreach (FileInfo f in diProfile.GetFiles())
{
    spwebsvcCopy.CopyResult crProfile = new ProfilePicUpdater.spwebsvcCopy.CopyResult();
    spwebsvcCopy.CopyResult[] crArrProfile = { crProfile };
    spwebsvcCopy.FieldInformation fiProfile = new ProfilePicUpdater.spwebsvcCopy.FieldInformation();
    fiProfile.DisplayName = f.Name;
    fiProfile.Type = ProfilePicUpdater.spwebsvcCopy.FieldType.Text;
    fiProfile.Value = f.Name;
    spwebsvcCopy.FieldInformation[] fiArrProfile = { fiProfile };

    using (FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read))
    {
        bytearrContent = new Byte[fs.Length];
        fs.Read(bytearrContent, 0, Convert.ToInt32(fs.Length));
    }

    try
    {
        //this overwrites file with same name
        strDestinationUrl = strURL + "/" + strProfileLibName + "/" + f.Name;
        uint intResult = wsCopy.CopyIntoItems(f.FullName,
                                new string[] { strDestinationUrl },
                                fiArrProfile,
                                bytearrContent,
                                out crArrProfile);
        Console.WriteLine(string.Format("{0} uploaded successfully",f.Name));        
    }
    catch (Exception ex)
    {
        Logger.Write(ex.Message);
    }
}

Reference:
http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems.aspx