0

First of all, I would like to ask question, do I need to insert image to Sharepoint as Image or byte[] array?

I have tried both ways to do that. In both cases it returns me System.ArgumentNullException. And there is not additional information, that I could make use of.

    SPUser user = web.EnsureUser(account);
    SPListItem item = items.Items.GetItemById(user.ID);

    web.AllowUnsafeUpdates = true;
    for (int i = 1; i < _propKeys.Length / 2; i++)
    {
       string val = person.Data.Where(a => a.Key == _propKeys[i, 0]).Select(a => a.Value).FirstOrDefault();

        if (_propKeys[i, 1] == "Image" && val != null)
        {
            item[_propKeys[i, 1]] = GetUserImage(acc);
        }
        else
            item[_propKeys[i, 1]] = val;

        item.Update();
     }
     web.AllowUnsafeUpdates = false;

To convert item to Image I have tried to use following code:

public static Image GetUserImage(string userName)
        {
            var directoryEntry = Values.enTry;
            var directorySearcher = new DirectorySearcher(directoryEntry);
            directorySearcher.Filter = string.Format("(&(SAMAccountName={0}))", userName);
            var user = directorySearcher.FindOne();

            var bytes = user.Properties["thumbnailPhoto"][0] as byte[];
            // I tried this byte array as well, got `NullReference` from there


            Image returnImage;
            using (var ms = new MemoryStream(bytes))
            {
                returnImage = Image.FromStream(ms);
            }
            return returnImage;
        }

I have hear, that I can't actually insert image straight to Sharepoint list, but I need to create library and from there take Image URL and insert that to list?

0

If you would like to upload an image (a file in general) to SharePoint you can send it either as stream or as byte array. Personally I prefer the latter one.

Instead of

var bytes = user.Properties["thumbnailPhoto"][0] as byte[];

try

byte[] bytes = (byte[])user.Properties["thumbnailPhoto"].Value;

See a similar solution with code samples here.

  • There is not property Value. And your link is broken, I think. Also could you tell, if I need to send Image or bytes to Sharepoint? – Taurib Mar 9 '15 at 10:06
  • The object 'user' must be an instance of the type 'DirectoryEntry'. It has a 'Properties' collection, via the indexer can one a single property (that is a 'PropertyValueCollection' type). The type 'PropertyValueCollection' should have a 'Value' property of type 'object'. Please note, that there is no extra integer indexer in my code (that is, not user.Properties["thumbnailPhoto"][0].Value)! – pholpar Mar 9 '15 at 11:06
0

Alright, got the solution for my problem. First of all, I needed to use image URL to insert it into list.

First of all, i got Image bytes from Active Directory:

        public static string GetUserImage(string userName)
        {
            var directoryEntry = Values.enTry;
            var directorySearcher = new DirectorySearcher(directoryEntry);
            directorySearcher.Filter = string.Format("(&(SAMAccountName={0}))", userName);
            var user = directorySearcher.FindOne();

            byte[] bytes = (byte[])user.Properties["thumbnailPhoto"][0] as byte[];

            return UploadImage(userName, bytes);
        }

Then I used these bytes to add Image as file and to insert into Sharepoint Library, that I made:

public static string UploadImage(string userName, byte[] img)
        {
            string loc = String.Empty;
            using (SPSite oSite = new SPSite(Values.SiteUrl))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPFolder myLibrary = oWeb.Folders["Profile"];

                    // Prepare to upload
                    Boolean replaceExistingFiles = true;

                    // Upload document
                    SPFile spfile = myLibrary.Files.Add(userName + ".jpg", img, replaceExistingFiles);
                    loc = (string) spfile.Item[SPBuiltInFieldId.EncodedAbsUrl];

                    // Commit 
                    myLibrary.Update();
                }
            }
            return loc;
        }

In the end I returned file location (full URL) and inserted that to list! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.