lundi 9 mars 2015

Insert image from ActiveDirectory to list c#



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. But value is there..



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;
}







0 commentaires:

Enregistrer un commentaire