views:

460

answers:

2

I need to find an user in a list to set the assignedto task property, these informations are in a list. So i use this method :

public static SPUser GetSPUser(SPListItem item, string key){ 
    SPFieldUser field = item.Fields[key] as SPFieldUser;

    if (field != null)
    {
        SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
        if (fieldValue != null)
        {
            return fieldValue.User;
         }
     }
     return null;
 }

The problem is that when i use this method or this part of code, my workflow stop without saying anything. Here an exemple of code when i use it :

using (SPSite site = new SPSite(adress_of_my_site))
{                
    using (SPWeb web = site.OpenWeb())
   {
        SPList list = web.Lists["Acteurs du projet"];
        SPView view = cobj_ListeDesActeursDuProjet.DefaultView;
        SPListItemCollection itemcollection = list.GetItems(view);
        foreach (SPListItem item in itemcollection)
        {                       
            SPUser lobj_acteur = Utilities.GetSPUser(item,"acteur");
            // Dictionary<string,class>
            ActeursDuProjet[item["Rôle"].ToString()] = 
                new ActeursDuProjet()
                { 
                 Login = lobj_acteur.LoginName, 
                 Email = lobj_acteur.Email 
                };
        }

    }


}

If i comment the content of my foreach my workflow continue as well...

If anybody have an idea it will be cool.

Regards, Loïc

edit: problem in the code

A: 

I tried commenting this above but it didn't format nicely so here it is.

It probably is fine, but this looks fishy to me:

// Dictionary<string,class>
ActeursDuProjet[item["Rôle"].ToString()] = 
    new ActeursDuProjet()
    { 
     Login = lobj_acteur.LoginName, 
     Email = lobj_acteur.Email 
    };

I would think it would be something like:

// dictionary declared somewhere earlier
Dictionary<string,ActeursDuProjet> roles = new Dictionary<string,ActeursDuProjet>();

// inside the foreach
string role = item["Rôle"].ToString();
if (!roles.ContainsKey(role))
{
    ActeursDuProjet foo = new ActeursDuProjet();
    foo.Login = lobj_acteur.LoginName;
    foo.Email = lobj_acteur.Email;
    roles.Add(role, foo);
}
Kit Menke
yeah it could be this too :) i just use object initializers from 3.5 framework for the class.But the problem don"t seem to be about that :)
LoKtO
Well the main thing I saw was "ActeursDuProjet[item["Rôle"].ToString()]". What collection is this modifying (it looks like it is storing it in something called ActeursDuProjet which is the same name as your class)?
Kit Menke
I make a mistake my dictionary is named Acteurs the definition : Public IDictionary<string, ActeursDuProjet> cobj_ActeursDuProjet = new Dictionary<string, ActeursDuProjet>();I use this dictonary to store this list :http://img53.imageshack.us/img53/1817/problemesharepoint.jpgI want to store the loginname ( for asignedto properties of tasks)and the email of the user who is in the first field of the list.
LoKtO
+1  A: 

Here are some debugging tips that might help:

ULS logs

Any exceptions should be reported here in some detail.

Enable debugging for all .NET code

This will cause the debugger to break whenever an exception occurs in SharePoint as well as your code. The downside is that the debugger will break on 'normal' exceptions that cause no side effects. So don't be misled!

To enable: Go to Debug, Exceptions and tick Common Language Runtime Exceptions. Also go to Tools, Options, Debugging and untick Enable Just My Code. Then attach to w3wp.exe.

Commenting code

You could also comment out all of your code. If the workflow step fails, you know there is a problem elsewhere. If the workflow step passes, then start uncommenting code until it fails - then you know where to look.

Alex Angas
nice :) My class are not marked as serializable so i have a serializationExceptionFive minutes ago i try to comment my code and i found that the problem come from my dictionary or my class :)
LoKtO
Great! Glad I could help :)
Alex Angas