2

I'm trying to update SiteUrl field during ItemAdding event. The field is Type="URL". I use this code in Event Receiver, but after user added new item, the field is still empty. (User cannot set this field himself. It has ShowInNewForm="FALSE" ShowInEditForm="FALSE").

    public override void ItemAdding(SPItemEventProperties properties)
    {
        SPFieldUrlValue urlValue = new SPFieldUrlValue();
        urlValue.Description = "site description...";
        urlValue.Url = properties.Web.Url;
        properties.AfterProperties["SiteUrl"] = urlValue;

    }

Is there some method to save properties, or am I missing something obvious here?

EDIT: turned out, this code is correct, only field name had typo. I found it out only after adding try-catch and debugging inside code.

1

Try to put your code code in try-catch block or debug your solution from visual studio. I suppose "SiteUrl" field is not in AfterProperties collection because it is not changing by user.

You do not filter values or cancel item adding, so maybe ItemAdded event is a possible way out? It's much easier to use it.

| improve this answer | |
  • I agree with doing this in ItemAdded, directly on the item instead of on the AfterProperties, seems like a better solution! – Robert Lindgren Nov 8 '13 at 15:00
  • Yes, turned out, that code was correct, only field name had typo. I found it out only after adding try-catch and debugging inside code. – Tschareck Nov 11 '13 at 9:00
1

try this instead:

public override void ItemAdding(SPItemEventProperties properties)
{
    base.EventFiringEnabled = false;

    SPFieldUrlValue urlValue = new SPFieldUrlValue();
    urlValue.Description = "site description...";
    urlValue.Url = properties.Web.Url;
    properties.AfterProperties["SiteUrl"] = urlValue;
    properties.Listitem.update();

    base.EventFiringEnabled = true;

}

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverbase.eventfiringenabled.aspx

in item added the code will look somthing like this:

public override void ItemAdded(SPItemEventProperties properties)
{

SPListItem listLink = properties.Listitem;

SPFieldUrlValue value = new SPFieldUrlValue();

value.Description = "test";

value.Url = "http://www.microsoft.com/sharepoint";

listLink["URL"] = value;

listLink.Update();

}
| improve this answer | |
  • Would not properties.listItem be null in itemadding, since the item is not yet created. Or am I just Friday tired ;) – Robert Lindgren Nov 8 '13 at 16:44
  • I think its Friday tired ;) its in added not adding :) – Ali Jafer Nov 9 '13 at 0:12
  • But, the almost last line in adding is surely a listItem.update(), even this Saturday ;) – Robert Lindgren Nov 9 '13 at 8:15
0
public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            using (SPWeb web = properties.OpenWeb())
            {
                try
                {

                    SPFieldUrlValue urlValue = new SPFieldUrlValue();
                    urlValue.Description = "Description will appear in the ink";
                    urlValue.Url = properties.RelativeWebUrl + "/_layouts/15/../Page.aspx?Id=" + properties.ListItem.ID;
                    properties.ListItem["Link"] = urlValue;

                    properties.ListItem.Update();
                    base.ItemAdded(properties);
              }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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