Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top