Question

I have tried to use an Event Receiver with ItemAdding event to set the Title field of a Document, so that when a end user uploads a document and is presented with the Edit Item page where they are forced to fill in required fields...I can populate the Title field or other fields.

I have used:

properties.AfterProperties["Title"] = "blah";
base.ItemAdding(properties);

This does not come up in the Title field in the form on the page. It is definately triggering the Event Receiver method as I've had it break on the break point.

I created my own field called "Container Record Number" with internal name "Container_x0020_Record_x0020_Number" interestingly this didn't work:

properties.AfterProperties["Container_x0020_Record_x0020_Number"] = "blah";
base.ItemAdding(properties);

but this did!

properties.AfterProperties["Container Record Number"] = "blah";
base.ItemAdding(properties);

So DisplayName seems to be the key here...and it looks like Title is doing something special in the base.ItemAdding(properties) for some reason.

Any suggestions?

ItemAdded will work by saving the "blah" into the Title but not until after the end user has clicked OK and saved the Edit Item page.

Was it helpful?

Solution

enter link description hereI always do this with JavaScript (now always jQuery) based on values passed on the Query String. I know, not "real" development, but it works! What are you basing the Title value on?

Well, if you're going to call it a "hack"... ;-) This article was the basis for all of my thinking on this: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx

jQuery selectors make finding the objects on the page so much easier. Something like: $("input:[Title='Title']") is all you need to find the input field.

I have yet to convert this little bit to jQuery, but here's the Query String grabbing code:

var vals = new Object();
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
for (var i=0; i < args.length; i++) {
  var nameVal = args[i].split("=");
  var temp = unescape(nameVal[1]).split('+');
  nameVal[1] = temp.join(' ');
  vals[nameVal[0]] = nameVal[1];
}

and yes, I copy EditForm.aspx and add the script into the copy, then set the copy as the supporting form for the list.

M.

EDIT 2011-08-04 I added a function to SPServices called SPGetQueryString called for this a long while back.

OTHER TIPS

This is a common problem with the ItemAdding / ItemUpdating event receivers when uploading Office 2007 documents.

The XML document parser will not honour the Internal Name as an indexer into the AfterProperties collection. Instead, it appears to use the Title as an indexer in most cases. Even better than the title is the XmlConvert.Decode(InternalName) which seems to work consistently.

My understanding is that the xml parser disregards the after properties changes in view of the properties from the 2007 document being promoted to the SharePoint fields, unless the after properties are changed properties.

It gets more interesting in the ItemUpdating event receiver with more fields being cleared / reverting to document values but I don't have a clear understanding of the rules the document parser uses to evaluate property promotion. Ideally, I would love to have a setting that disables field promotion SPECIFICALLY for my custom field because I am setting the value in the after properties.

Try using BeforeProperties. Also the hashtable uses display names for keys just like SPListItemVersion.

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