0

I must set a Value in Items Adding, it should be start Value of element. How could I set a Value. Like Default Value But that will be number User1, User2, User3 To Unical identyfy User.

I have got code, how to set start value of elementco list "Jawna":

    /// </summary>
    public override void ItemAdded(SPItemEventProperties properties)
    {
        this.EventFiringEnabled = false;
        base.ItemAdded(properties);
        if (properties.List.Title == "Jawna")
        {
            using (SPSite site = new SPSite(properties.WebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList FirstItem = properties.Web.Lists["Jawna"];
                    SPList lstOtherList = properties.Web.Lists["Niejawna"];
                    SPListItem item = lstOtherList.Items.Add();
                    web.AllowUnsafeUpdates = true;
                    item["UnikalnaNazwa"] = "User" + Convert.ToInt32((FirstItem.ItemCount)+1);
                    item["Title"] = properties.AfterProperties["Title"].ToString();
                    item["B"] = properties.AfterProperties["B"].ToString();
                    item["Status"] = properties.AfterProperties["Status"].ToString();
                    item.Update();
                }
1
  • First of all, the code above is an ItemAdded event receiver, not an ItemAdding (or you have missed the point of naming your methods correctly) May 26, 2015 at 11:18

1 Answer 1

0

I found this link always to be very useful, when it came to understand SharePoint EventReceiver. Looking a the link, you'll find out that it's better to use properties.ListItem["Title"] instead of Afterproperties, because that way you'll get a value for sure. But it should be working anyway (if a value is being added in the first item). Also you shouldn't forget to set "web.AllowUnsafeUpdates = false;" at the end.

From what I see you're trying to add a new item to List B for every item which gets added to List A. Besides some optimizations which jump to my eye, I don't see why your code shouldn't be working.

Could you please explain your problem in more detail and what you're trying to achieve.

4
  • I mast have a set start value when the popap show on the screen.
    – Grzegorz Z
    May 26, 2015 at 9:40
  • If you want to have a default value for a certain column/field when you open up the NewForm.aspx you have to use Javascript as there is no way to achieve this using the SharePoint Server Object Model. Have a look at this link to see if this fits your needs.
    – Andreas
    May 26, 2015 at 11:39
  • But how. Could You give any Example of code please?
    – Grzegorz Z
    May 26, 2015 at 11:52
  • Could you please follow the link and read what's written there? It's part of the dynamic forms and contains all the required information to do what you want. I don't think it's necessary to paste the code here.
    – Andreas
    May 26, 2015 at 12:27

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.