I try to create a document in a user control. The code to create the item looks like this:
Dim td AsDocumentType = DocumentType.GetByAlias("eventItem") Dim author AsUser = User.GetUser(0) Dim doc AsDocument = Document.MakeNew(txtEventName.Text, td, author, 1119) doc.getProperty("bodyText").Value = rteDescription.Content doc.getProperty("startDate").Value = txtDate.Text doc.getProperty("showOnHomepage").Value = False doc.Publish(author) umbraco.library.UpdateDocumentCache(doc.Id)
The documenttype alias "eventItem" is configured to be the "itemDocType". The newly created document is now sorted to the create date rather than the configured itemDateProperty (witch should be the "startDate" Property).
If I cretae an document in the umbraco backend all is working fine. If I try to call the document objects .save() methode the following error occurs:
[NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.] InfoCaster.Umbraco.DateFolders.DateFolders.Document_AfterSave(Document doc, SaveEventArgs e) +1216 umbraco.cms.businesslogic.web.SaveEventHandler.Invoke(Document sender, SaveEventArgs e) +0 umbraco.cms.businesslogic.web.Document.FireAfterSave(SaveEventArgs e) +76 umbraco.cms.businesslogic.web.Document.Save() +201 MemberAddEvent.cmdSave_Click(Object sender, EventArgs e) in ...\MemberAddEvent.ascx.vb:30 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
@Jan; this is an error happening in my DateFolders package, not just an umbraco bug ;-)
@Michael; I don't know what's wrong. The best way to fix this is by including the DateFolders source instead of the binary in your project and use Visual Studio to debug. The debugger will kick in when an error occurs. That way you can inspect the error and find out what object is null. When you do find out, please post the problem more specifically here please :-)
I could start to debug my problem. In the sub Document_AfterSave() the last three lines are causing my problem:
doc = new Document(doc.Id);
BasePage .Current.ClientTools.
ChangeContentFrameUrl(string.Concat("editContent.aspx?id=", doc.Id));
What ist the doc = new Document(doc.Id) good for? As I understand, it creates/loads the same document again? Does this make any sense? After this line, I get the NullReference Exeption if it tries to get some value of the object (doc.Id in next line).
If I'm correct, I don't need last command because my document is created by code and not in the backend, so set any URL in the backend makes no sense for me.
I'm having the exact same problem Michael; setting the date using the Document API throw an error on save/publish. Has anyone come up with a solution for this?
** Update **
Seems like the DateTime is still actually getting added to the node, despite the error. Swallowing the exception does the trick. Only saw that the solution was mentioned above.
Anyway, the following code works for me (as of Umbraco 4.11.1):
var umDoc = Document.MakeNew(library.StripHtml(heading), dt, author, mediaReleasesNode.Id);
var summary = library.StripHtml(library.TruncateString(strPTags, 300, "..."));
Thanks GFoley83, I had the same issue too in Umbraco 4.9.1. For me it looks like the error is being thrown when I do a .save()
var displayDate = new DateTime(year, month, day, hour, minute, second);
//to modify the document properties....
mdoc.getProperty("displayDate").Value = displayDate;
// After modifying the document, prepare it for publishing
mdoc.Publish(new umbraco.BusinessLogic.User(0));
try
{
mdoc.Save();
}
catch (Exception e)
{
//do nothing, this is a "fix" per the creator of the datefolders package
// our.umbraco.org/projects/developer-tools/datefolders/bug-reports/17603-Error-if-create-an-document-and-call-docsave()
// result = e.StackTrace.ToString() + "<br /><br />" + e.Message;
}
Error if create an document and call doc.save()
I try to create a document in a user control. The code to create the item looks like this:
The documenttype alias "eventItem" is configured to be the "itemDocType". The newly created document is now sorted to the create date rather than the configured itemDateProperty (witch should be the "startDate" Property).
If I cretae an document in the umbraco backend all is working fine. If I try to call the document objects .save() methode the following error occurs:
Hi Michael
I'm not sure why the above happens but have you made sure that both the .dll files and usercontrols have been copied over into your site?
Maybe this wiki-entry can help? http://our.umbraco.org/wiki/reference/api-cheatsheet/creating-a-document
/Jan
@Jan; this is an error happening in my DateFolders package, not just an umbraco bug ;-)
@Michael; I don't know what's wrong. The best way to fix this is by including the DateFolders source instead of the binary in your project and use Visual Studio to debug. The debugger will kick in when an error occurs. That way you can inspect the error and find out what object is null.
When you do find out, please post the problem more specifically here please :-)
Thanks for the hits, I will try to solve the problem this way and poste here if I find some more infos.
I could start to debug my problem. In the sub Document_AfterSave() the last three lines are causing my problem:
What ist the doc = new Document(doc.Id) good for? As I understand, it creates/loads the same document again? Does this make any sense? After this line, I get the NullReference Exeption if it tries to get some value of the object (doc.Id in next line).
If I'm correct, I don't need last command because my document is created by code and not in the backend, so set any URL in the backend makes no sense for me.
I'm not sure anymore why I'm reloading the document... Should have an explanation :P
Anyway, you can surround that part with a try/catch block or do fix it the nice way by checking for null values.
I'm having the exact same problem Michael; setting the date using the Document API throw an error on save/publish.
Has anyone come up with a solution for this?
** Update **
Seems like the DateTime is still actually getting added to the node, despite the error. Swallowing the exception does the trick. Only saw that the solution was mentioned above.
Anyway, the following code works for me (as of Umbraco 4.11.1):
Thanks GFoley83, I had the same issue too in Umbraco 4.9.1. For me it looks like the error is being thrown when I do a .save()
is working on a reply...