Object
reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and
where it originated in the code.
Exception
Details: System.NullReferenceException: Object reference not set to an instance
of an object.
Am
I missing something or did I take wrong approach to achieve desired goal?
Raised when ContentService.Save is called in the API.
NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default).
"sender" will be the current IContentService object.
"e" will provide:
NOTE: If the entity is brand new then HasIdentity will equal false.
SavedEntities: Gets the collection of IContent objects being saved.
NullReferenceException might be caused by a Content that does not have a property alias "templateName".
It's better to check the property before retrieve the value.
if (node.ContentType.Alias == "documentType")
{
if (node.HasProperty("templateName"))
{
var emailTemplateName = node.GetValue("templateName").ToString();
}
}
It turns out, that I was trying to retrieve property value with wrong property alias. I do not know why, but all property aliases on document type were written in lower case, not in camel case as they usually are.
How to get content property value in Saving / Saved / Publishing / Published events?
I would like to get property values in Umbraco ApplicationEventHandlers.
I'm using Umbraco v.: 7.18
My code looks like:
public class UmbracoEventHandlers: ApplicationEventHandler
{
public UmbracoEventHandlers()
{
ContentService.Saving += ContentService_Saving;
ContentService.Saved += ContentService_Saved;
ContentService.Publishing += ContentService_Publishing;
}
void ContentService_Saved(IContentService sender, SaveEventArgs e)
{
foreach (var node in e.SavedEntities)
{
if (node.ContentType.Alias == "documentType")
{
string emailTemplateName = node.GetValue("templateName");
…
GetValue method call always throws error:
An error occurred
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Am I missing something or did I take wrong approach to achieve desired goal?
Any help would be appreciated.
Hi Pavel,
Did you read that article ?
http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events
About Saving event :
Raised when ContentService.Save is called in the API. NOTE: It can be skipped completely if the parameter "raiseEvents" is set to false during the Save method call (true by default). "sender" will be the current IContentService object. "e" will provide: NOTE: If the entity is brand new then HasIdentity will equal false. SavedEntities: Gets the collection of IContent objects being saved.
How did you call Save method ?
Thanks
NullReferenceException might be caused by a Content that does not have a property alias "templateName". It's better to check the property before retrieve the value.
Thank you both for help.
It turns out, that I was trying to retrieve property value with wrong property alias. I do not know why, but all property aliases on document type were written in lower case, not in camel case as they usually are.
is working on a reply...