0

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

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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