Get Current Node In Custom Data Type (IUsercontrolDataEditor)
I have a document type that has a textstring field. I created a second field using a custom data type (one that implements IUsercontrolDataEditor and that makes use of umbraco usercontrol wrapper as the render control). I want that second field to be able to use the value in the first field to construct a URL.
My problem is that I can't seem to programmatically access the other fields on the node the user is currently editing (in the Content section of the Umbraco interface). I tried umbraco.presentation.nodeFactory.Node.GetCurrent() and umbraco.presentation.UmbracoContext.Current.PageId, but they both return null (actually, the first call seems to return an exception).
Any idea how I can access the node currently being edited in Umbraco?
We are talking some kind of extention of the backoffice?
The NodeFactory is used on the frontend, in your macros and gets all its data from the cache, not from the database. The documents are loaded from the database and when they are saved, the cache gets updated.
Nodes - From the cache, should be used in front end macros
Document - From the database, used to update properties of content nodes.
I'm not sure but if you are building a data editor/data type it should be independent from other properties of a certain content node. But if you really need to do it this way i would look in the source-code of "/umbraco/editContent.aspx" and se how the id is stored (or get the QueryString directlly) and then open the document from threre but it sounds like a dirty solution.
I have similar need. I created a new upload datatype and I've added some javascript to the datatype's .ascx to get the values of all the UMB properties on the editContent.aspx when the user clicks the "select file" button (to persist the data via cookies), however the JS seems to not fire. I know jQuery is available because there's a umb include control on the editContent.aspx referencing jQuery 1.6.2. So thinking the jQuery was somehow not available to my datatype I added a ref to jQuery in the datatype itself and when I do this, everytime I add text to any of the properties on the page the editContent.aspx forces a Publish (as if there's a keyup trigger to force a document publish). So thinking there's an obvious jQuery clash I remove my jQuery reference from the datatype and I'm back to square 1 again wth my javascript not firing. Is there a way to get the property values before the node/document is published?
I did a search on this before posting here and I came across answers similar to those you all provided. I don't understand why you say to get the ID from the query string. When I'm editing content, the URL is always http://wwwud.mysite.com/umbraco/#content regardless of which node I'm editing (i.e., there are no query string values).
Also, what good does it do me to know the difference between a node and a document when I can't access either or the current node/page ID to instantiate a document or node from?
Thanks, Markus, I'll take a look at editContent.aspx to see if it has anything of use. However, I really would think the API would offer some way to access the document/node currently being edited.
Also, to clarify, the user is typing into field 1 a value like "~/somePage.aspx" and in field 2 I want to display http://www.mysite.com/us-en/somePage.aspx. I could surely just combine them into one field, but I don't want to do that, as this is just one example (of many possible examples) of why I'd want to access other fields from custom data type fields.
To access the current document from a datatype, you can request the "id" querystring:
Document d = new Document(HttpContext.Current.Request.QueryString["id"]); string x = d.getProperty("yourProperty") != null ? d.getProperty("yourProperty").Value.ToString() : "";
Note this gets the document from the DB, it might not be "in synch" after your user starts making changes, you might need to re-initialize it after a save.
FYI, if you do a string currentNodePageID = Request.QueryString["id"].ToString(); you'll get the current node id. Only after a save and publish would you be able to access the Node/Doc. I believe the marco videos reveals you can accomplish the same by:
doc.Save(); doc.Publish(doc.User); umbraco.library.UpdateDocumentCache(doc.Id); // However this will not work for me as I'll need to get these vars before postback, so I have a work around to my problem but not as user friendly as being able to work with jQuery.
Thanks to Markus, Tom, and Kevin for mentioning and reiterating that I can get the current node/page ID from Request.QueryString("id"). It worked. I didn't think it would, because the query string was not apparent in the URL. Umbraco must inject that on the server-side.
FYI, I looked at the code in editContent.aspx.cs and it also using the query string value to get the ID:
override protected void OnInit(EventArgs e) { base.OnInit(e); //validate! int id; if (!int.TryParse(Request.QueryString["id"], out id)) { //if this is invalid show an error this.DisplayFatalError("Invalid query string"); return; } m_ContentId = id; //... }
Great Nicholas! Just to make things clear and for you to sleep better =)
The backoffice is buildt with frames, that means that the url you see in your browser is not always the current noded page. If you right click in the area you are interesed in and choose "Properties" or something like that you can look closer at that frame.
This is in FF, sorry for the Swedish language in menues etc.
Get Current Node In Custom Data Type (IUsercontrolDataEditor)
I have a document type that has a textstring field. I created a second field using a custom data type (one that implements IUsercontrolDataEditor and that makes use of umbraco usercontrol wrapper as the render control). I want that second field to be able to use the value in the first field to construct a URL.
My problem is that I can't seem to programmatically access the other fields on the node the user is currently editing (in the Content section of the Umbraco interface). I tried umbraco.presentation.nodeFactory.Node.GetCurrent() and umbraco.presentation.UmbracoContext.Current.PageId, but they both return null (actually, the first call seems to return an exception).
Any idea how I can access the node currently being edited in Umbraco?
I'm using Umbraco 4.7.something (4.7.1 maybe).
Hi!
We are talking some kind of extention of the backoffice?
The NodeFactory is used on the frontend, in your macros and gets all its data from the cache, not from the database. The documents are loaded from the database and when they are saved, the cache gets updated.
Nodes - From the cache, should be used in front end macros
Document - From the database, used to update properties of content nodes.
I'm not sure but if you are building a data editor/data type it should be independent from other properties of a certain content node. But if you really need to do it this way i would look in the source-code of "/umbraco/editContent.aspx" and se how the id is stored (or get the QueryString directlly) and then open the document from threre but it sounds like a dirty solution.
Have a look at this wiki for more info: http://our.umbraco.org/wiki/reference/api-cheatsheet/difference-between-node-and-document
Jeroen
I have similar need. I created a new upload datatype and I've added some javascript to the datatype's .ascx to get the values of all the UMB properties on the editContent.aspx when the user clicks the "select file" button (to persist the data via cookies), however the JS seems to not fire. I know jQuery is available because there's a umb include control on the editContent.aspx referencing jQuery 1.6.2. So thinking the jQuery was somehow not available to my datatype I added a ref to jQuery in the datatype itself and when I do this, everytime I add text to any of the properties on the page the editContent.aspx forces a Publish (as if there's a keyup trigger to force a document publish). So thinking there's an obvious jQuery clash I remove my jQuery reference from the datatype and I'm back to square 1 again wth my javascript not firing. Is there a way to get the property values before the node/document is published?
I did a search on this before posting here and I came across answers similar to those you all provided. I don't understand why you say to get the ID from the query string. When I'm editing content, the URL is always http://wwwud.mysite.com/umbraco/#content regardless of which node I'm editing (i.e., there are no query string values).
Also, what good does it do me to know the difference between a node and a document when I can't access either or the current node/page ID to instantiate a document or node from?
Thanks, Markus, I'll take a look at editContent.aspx to see if it has anything of use. However, I really would think the API would offer some way to access the document/node currently being edited.
Also, to clarify, the user is typing into field 1 a value like "~/somePage.aspx" and in field 2 I want to display http://www.mysite.com/us-en/somePage.aspx. I could surely just combine them into one field, but I don't want to do that, as this is just one example (of many possible examples) of why I'd want to access other fields from custom data type fields.
Hi,
To access the current document from a datatype, you can request the "id" querystring:
Note this gets the document from the DB, it might not be "in synch" after your user starts making changes, you might need to re-initialize it after a save.
-Tom
@Nicholas
FYI, if you do a string currentNodePageID = Request.QueryString["id"].ToString(); you'll get the current node id. Only after a save and publish would you be able to access the Node/Doc. I believe the marco videos reveals you can accomplish the same by:
doc.Save();
doc.Publish(doc.User);
umbraco.library.UpdateDocumentCache(doc.Id); // However this will not work for me as I'll need to get these vars before postback, so I have a work around to my problem but not as user friendly as being able to work with jQuery.
Thanks to Markus, Tom, and Kevin for mentioning and reiterating that I can get the current node/page ID from Request.QueryString("id"). It worked. I didn't think it would, because the query string was not apparent in the URL. Umbraco must inject that on the server-side.
FYI, I looked at the code in editContent.aspx.cs and it also using the query string value to get the ID:
So that seems like the way to go.
Great Nicholas! Just to make things clear and for you to sleep better =)
The backoffice is buildt with frames, that means that the url you see in your browser is not always the current noded page. If you right click in the area you are interesed in and choose "Properties" or something like that you can look closer at that frame.
This is in FF, sorry for the Swedish language in menues etc.
Now it makes perfect sense! Thanks, Markus!
is working on a reply...