Question

I am trying to populate SiteUrl field in itemAdded event, this code doesn't give me error but field is not get populated

  public override void ItemAdded(SPItemEventProperties properties)
    {
     string projectType = properties.ListItem["ProjectTypes"].ToString().Replace(" ", "");
     string webUrl = properties.ListItem["ProjectUrl"].ToString();
     string webTitle = properties.ListItem["Title"].ToString();
     SPFieldUrlValue field = new SPFieldUrlValue();
     field.Description = "Details";
     field.Url = "/Sites/SiteDemo/" + projectType + "/" + webUrl;
     properties.ListItem["SiteUrl"] = field; // field value not populated
     base.ItemAdded(properties); }
Was it helpful?

Solution

I think your mistake is to not call properties.ListItem.SystemUpdate(); after setting the field value on the item:

public override void ItemAdded(SPItemEventProperties properties)
{
    string projectType = properties.ListItem["ProjectTypes"].ToString().Replace(" ", "");
     string webUrl = properties.ListItem["ProjectUrl"].ToString();
     string webTitle = properties.ListItem["Title"].ToString();
     SPFieldUrlValue field = new SPFieldUrlValue();
     field.Description = "Details";
     field.Url = "/Sites/SiteDemo/" + projectType + "/" + webUrl;
     properties.ListItem["SiteUrl"] = field; // field value not populated
     var oldEventFiring = EventFiringEnabled;
     EventFiringEnabled = false;
     properties.ListItem.SystemUpdate();
    EventFiringEnabled = oldEventFiring 
     base.ItemAdded(properties); 
}

also notice the EventFiringEnabled, stopping a new ItemAdded to be called on update

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top