Error the URL '' is invalid. When using ItemsUpdated in a photo lib
- I'm trying to change the name of a file when a user Uploads an image using itemupdated.
Look at the below:
Before adding the try{}catch{}
I was getting error :
Save Conflict
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.
Now i get the error(When checking the file in):
The URL 'PhotoLib/interfaa.jpg' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
public override void ItemUpdated(SPItemEventProperties properties)
{
//base.ItemUpdated(properties);
DisableEventFiring();
SPListItem doc = properties.ListItem;
//I had to use AfterProperties because i needed to get the value from a dropDownlist before checking the file in unless its null
string sn = properties.AfterProperties["StoreName"].ToString();
doc["Name"] = String.Format("{0}_{1}", sn, sn);
try
{
doc.Update();
EnableEventFiring();
}
catch (SPException spEx)
{
if (!spEx.Message.ToLower().Contains("save conflict"))
{
throw spEx;
}
}
}
Sure i need to do something in my ItemUpdated event but still doing some research.
Any ideas???
Réponses
- Hi Patrick,
Have you checked using the debugger that your code is being called and there is no exception?
I suggest to always use a try / catch block within the receiver, and trace out (System.Diagnostics.Trace, http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx) the exceptions if happen. Also, it is good practice to trace out each entry and exit points in you receiver.
Peter- Marqué comme réponsePatrick.I mardi 30 juin 2009 00:44
Toutes les réponses
Hi Patrick,
The way you "handle" the non-wished exception is not ideal. The original exception is still there, although it is not shown up to the user interface.
You can try:
SPWeb web = properties.OpenWeb();
SPSite site = web.Site;
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
// do the job
web.AllowUnsafeUpdates = false;
site.AllowUnsafeUpdates = false;
You should put the last two lines as well as the EnableEventFiring() in a finally block.
Peter- Well, and at the end of the finally block you should call the base.ItemUpdated(properties) too.
A possible workaround is to use ItemUpdating (no need to call doc.Update() explicitly!), and set the
properties.AfterProperties["Name"] = String.Format("{0}_{1}", sn, sn);
Call the base.ItemUpdating(properties).
Peter Hi Patrick,
Please, let me know the status of your issue!
If it was helpful or answers your question, please mark it as such. If you need more help on the topic, please, let us know how we can help you!Thanks!
Peter
- Thanks Peter.
I haven't gone back to it yet when i get back to it and test it i will let you know
Cheers - Hi Patrick,
Thanks in advance!
Peter - Hi Peter,
I tried the below but its not doing anything...
public override void ItemUpdating(SPItemEventProperties properties)
{
SPListItem doc = properties.ListItem;
string sn = doc["FileLeafRef"].ToString();
properties.AfterProperties["Name"] = String.Format("{0}_{1}", sn, sn);
base.ItemUpdating(properties);
}
- Hi Patrick,
Have you checked using the debugger that your code is being called and there is no exception?
I suggest to always use a try / catch block within the receiver, and trace out (System.Diagnostics.Trace, http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx) the exceptions if happen. Also, it is good practice to trace out each entry and exit points in you receiver.
Peter- Marqué comme réponsePatrick.I mardi 30 juin 2009 00:44
- Hi Peter,
I still get error
Save Conflict
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.
and then
The URL 'MyPicLib/Dock.jpg' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
When i do the following:
The image does get uploaded and the image name does change but i always have to go back and CHECK IT IN since i get the error on the preview page when i click
CHECK IN.
Code below:
public override void ItemUpdated(SPItemEventProperties properties)
{
DisableEventFiring();
SPWeb web = properties.OpenWeb();
SPSite site = web.Site;
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
SPListItem doc = properties.ListItem;
string sn = properties.AfterProperties["StoreName"].ToString();
doc["Name"] = String.Format("{0}_{1}", sn, sn);
doc.Update();
base.ItemUpdated(properties);
web.AllowUnsafeUpdates = false;
site.AllowUnsafeUpdates = false;
EnableEventFiring();
}
- Hi Patrick,
Which line of code throws the exception(s)? How do you get the second one?
I suggest to slightly modify your code like this:
using System.Diagnostics;
public override void ItemUpdated(SPItemEventProperties properties)
{
try
{
Trace.TraceInformation("Entering ItemUpdated");
DisableEventFiring();
SPWeb web = properties.OpenWeb();
SPSite site = web.Site;
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
SPListItem doc = properties.ListItem;
string sn = properties.AfterProperties["StoreName"].ToString();
string newName = String.Format("{0}_{1}", sn, sn);
doc["Name"] = newName;
Trace.TraceInformation("Try to set Name to {0}", newName);
doc.Update();
Trace.TraceInformation("Item updated");
base.ItemUpdated(properties);
Trace.TraceInformation("Base ItemUpdated finished");
}
catch(Exception ex)
{
Trace.TraceError("Exception in ItemUpdated. Message: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace);
}
finally
{
web.AllowUnsafeUpdates = false;
site.AllowUnsafeUpdates = false;
EnableEventFiring();
Trace.TraceInformation("Exit from ItemUpdated");
}
}
"The image does get uploaded and the image name does change"
Then it seems your event handler do its job.
You wrote you got the error when doing check-in. Isn't there any event handler for ItemCheckinIn (or ItemCheckedIn)?
Peter Hi Peter,
Thanks alot for the replies
I decided to sit and get it working today.
I used doc.SystemUpdate(false); instead of //doc.Update();
And i stopped getting the error :) and IT WORKED
It seems SystemUpdate doesn't update the modified version
See this http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.systemupdate.aspx
Is this a bug?
Also i have lookup field column the users have to select before they checkin an image but i'm trying to use this field to modify the image name.
if i do
string sn = properties.AfterProperties["LookupFieldName"].ToString();
It doesn't get it i reckon its null.
But it works on other normal fields..
Any ideas?Hi,
"It seems SystemUpdate doesn't update the modified version"
It is by design AFAIK. Or you meant it is a bug that it's not working with simple Update? Well, maybe, but we should know more about the case to decide that.
To get or modify the value of a lookup field you should work with the SPFieldLookupValue (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookupvalue.spfieldlookupvalue.aspx) for a single choice, or with the SPFieldLookupValueCollection () for a multi choice lookup field.
It is important to use the internal name of the field when you use the AfterProperties, like it is shown here for a single choice lookup field:
ItemAdding() Getting Values from DropDown List on Edit Form
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/25c91cbf-5c56-42b8-b2b4-4434cef1ff31
You may receive null because of not using the internal field name (but I would assume an exception instead in this case), or the field may have no value (no selection).
Here is an example about the multi choice lookup:
String fieldValue = Convert.ToString(properties.AfterProperties[internalFieldName]);
SPFieldLookupValueCollection myLookUps = new SPFieldLookupValueCollection(fieldValue);foreach (SPFieldLookupValue myLookUp in myLookUps)
{
Trace.TraceInformation("Id: {0}; Value: {1}", myLookUp.LookupId, myLookUp.LookupValue);
}
Peter- Hi Peter and Thanks
I used something like this below:-
SPList list = web.Lists[properties.ListId];
SPField field = list.Fields[LookUpColumnName];
object fieldVal = properties.AfterProperties[field.InternalName];
SPFieldLookupValue fieldValue = new SPFieldLookupValue(properties.AfterProperties[field.InternalName].toString());
string ot= fieldVal.ToString();
Also used
string ot= fieldValue .LookupValue.ToString();
But no luck.I get something like this :- StoreName_someNumber..
Do i need to use SPFieldLookupValueCollection ?
Thanks in Advance - Hi Patrick,
"string ot= fieldValue .LookupValue.ToString();"
You should not call the ToString() method, as the LookupValue is a string itself.
"Do i need to use SPFieldLookupValueCollection ?"
I don't think so, but when you attached the debugger, you can see, or you should know from the list fields if your field is multivalued or not.
"I get something like this :- StoreName_someNumber.."
Could you send an example for that (or more)? I really do not know what it might be.
I suggest you to test the code first from a console application (of course using the field value in that instead of the AfterProperties), just to make sure your code that reads the field value is correct. We also had issues with lookup and user fields in event receiver code, as their values from AfterProperties seem to not always parsed correctly by the field.
Peter - Thanks Peter i will try that
If i remember i think i tried the LookupValue alone and i didn't get any good result
I get something like this :- StoreName_someNumber.."
If you look at my old post i had:
SPListItem doc = properties.ListItem;
string sn = properties.AfterProperties["StoreName"].ToString();
string newName = String.Format("{0}_{1}", sn, sn);
doc["Name"] = newName;
So basically i need to changes the new name to StoreName_LookUpFieldName
So when i had the code to grab the LookUpField
I'm suppose to get :StoreName_LookUpFieldName
But as i said my new image name looks like this now :StoreName_SomeNumber e.g NewYork_3
So its possible values from AfterProperties seem to not always parsed correctly by the field as you stated..
Any ideas?
Hope you got me.
- Hi Peter...
Thisn has been quite interesting but i go the LookUpFields by doing?
SPFieldLookupValue Loc= new SPFieldLookupValue(doc["LOutLetLocation"].ToString());
int lookedUpItemID = Loc.LookupId;
string lookedUpItemString = Loc.LookupValue; // Got the text i needed here
I also wanted to include the current date with time so the file name would be changed to sname_loc_currentdatetime
string currentDate = DateTime.Now.ToString("yyyy-MM-dd"); but when i do yyyy-MM-dd HH:mm
I can get the date but not the time :(
string newName = String.Format("{0}_{1}_{2}", sn, loc, currentDateTime);
All is good but the time doesn't show up when i add the hours and minutes - Hi Patrick,
Well, this question seems to be a general .NET question, not SharePoint related, but do you really use
string currentDate = DateTime.Now.ToString("yyyy-MM-dd"); but when i do yyyy-MM-dd HH:mm
and not converting a date field valueof a list item to string? In the latter case your date field may be created as a date only field.
"the time doesn't show up when i add the hours and minutes"
Does it show zeros (like 2009-07-06 00:00) or it is missing (like 2009-07-06)?
Back to your previous post:
" I get something like this :- StoreName_someNumber.."
It is because you wrote out the field converted to string "as is" and did not used LookupId and LookUpValue. The simeNumber was the LookupId.
string newName = String.Format("{0}_{1}", sn, sn);
Would set newName for the same value (sn) separated with an underscore. I think it is not you would like to do.
Peter DateTime.Now.ToString does print out the hours ,minute and seconds.
The issue was with the concatenation it didn't like the :
I still get the error The URL 'PhotoLib/interfaa.jpg' is invalid
But after removing the
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
and
web.AllowUnsafeUpdates = false;
site.AllowUnsafeUpdates = false;
so only the doc.SystemUpdate(false); does work :(
Thanks

