PushCode
PushCode

Reputation: 3117

assign a value to a custom column when a document is added or created in a sharepoint document library

I thought this would be pretty simple, but guess not. I have the following code, it deploys and activates just fine, but when I add a new document to the document library, nothing happens. No errors either. And this is taken directly from another example from someone online who said it works.

Here's the code for my feature:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace DocNumGenerator
{
    class DocNumGenerator : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
        }

        public override void ItemAdding(SPItemEventProperties properties)
        {
            properties.AfterProperties["DocNum"] = "4321";
            base.ItemAdding(properties);
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
        }

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }

    }
}

Simple right! Why doesn't this work? Do I need to somehow specify the name of the document library in addition to the name of the custom column that I am specifying? I'm lost on this one and desperate for a solution. This is a SharePoint 2007 environment, publishing site.

Thanks for any help!

Upvotes: 0

Views: 1387

Answers (4)

PushCode
PushCode

Reputation: 3117

I got it to work..and I swear I'd tried this before. I also did a reboot, so maybe that was it???

Here's what is working for me now:

public override void ItemAdding(SPItemEventProperties properties)
        {
            this.DisableEventFiring();
            properties.AfterProperties["Document Number"] = "whatever";
            this.EnableEventFiring();
        }

Upvotes: 0

Rob Windsor
Rob Windsor

Reputation: 6859

Try this in the ItemAdded event handler:

DisableEventFiring();
SPListItem item = properties.ListItem;
item["DocNum"] = "4321";
item.Update();
EnableEventFiring();

Upvotes: 0

JMD
JMD

Reputation: 1408

Is DocNum the display name or the internal name? Try change between those.

Upvotes: 0

Colin
Colin

Reputation: 10638

could you try first calling base, base.ItemAdding(properties);, then just set properties.ListItem["DocNum"] and the call properties.ListItem.SystemUpdate(false);

Upvotes: 0

Related Questions