Edit property values programmatically Options
dan
Posted: Wednesday, February 13, 2008 1:21:56 PM
Rank: Enthusiast

Joined: 12/15/2007
Posts: 20
I have just managed to upload a bunch of pages into Umbraco and realised that they are using the incorrect template. How can I edit the template property, or any other property programmatically.

Up until now I have been adding whole pages to Umbraco and have not had to edit any content in bulk. If I want to change all the pages that are of a particular Document Type to use a different template where would I start?

Cheers,

Dan
PeterD
Posted: Wednesday, February 13, 2008 2:05:13 PM

Rank: Fanatic

Joined: 7/20/2006
Posts: 453
Location: NL
I struggled with the same, but its quite simple:

umbraco.BusinessLogic.User user = new umbraco.BusinessLogic.User(YOURUSERNAME, YOURPASSWORD);
Document post = new Document(NODEID OF DOCUMENT YOU WANT TO EDIT);
post.getProperty("YOURPRPERTY").Value = "your new value";
post.Publish(user);
umbraco.library.PublishSingleNode(post.Id);

HTH,
PeterD

Working on an events-calendar with recursion. Post requests on my blog!
dan
Posted: Wednesday, February 13, 2008 4:27:36 PM
Rank: Enthusiast

Joined: 12/15/2007
Posts: 20
Hi Peter,

That's great. The code works well for any properties I have defined myself. However, I need to set a different Template for particular pages (of a particular document type) and I'm not sure how to refer to the template field as it is not one I have created myself.

Code:

Dim user As New umbraco.BusinessLogic.User("username", "password")
Dim post As New Document(doc number as int here)
post.getProperty("property name here").Value = "value to be changed"
post.Publish(user)
umbraco.library.PublishSingleNode(post.Id)


I have tried the above code (converted to vb.net from your c#) which works fine when I know the name of the property.

Do you have any idea how I refer to the template field dropdown so as to change it?

I've tried "Template" and "NodeTypeAlias" but I get a NullReferenceException. Is the template referenced in a different fashion, or have I just got the property name wrong?

Thanks for your help.

Dan
PeterD
Posted: Wednesday, February 13, 2008 5:37:47 PM

Rank: Fanatic

Joined: 7/20/2006
Posts: 453
Location: NL
I havent tested this, but try to add this:

DocumentType dt = DocumentType.GetByAlias("your new documenttype alias");
post.Template = dt.Id;


HTH,
PeterD

Working on an events-calendar with recursion. Post requests on my blog!
dan
Posted: Wednesday, February 13, 2008 8:28:08 PM
Rank: Enthusiast

Joined: 12/15/2007
Posts: 20
Hi Peter,

Code:
DocumentType dt = DocumentType.GetByAlias("your new documenttype alias";

Unfortunately this didn't return the Id of the Template.

Code:
post.Template = 1234;

However, this bit did the trick. As long as I know the Id of the Template in advance this works well.

Thanks for your help. You've saved me a whole heap of manual updating.

Dan
dan
Posted: Wednesday, February 13, 2008 8:29:55 PM
Rank: Enthusiast

Joined: 12/15/2007
Posts: 20
Weirdness on post update.

This worked (1234 being the Id of the template).

Code:
post.Template = 1234;
PeterD
Posted: Wednesday, February 13, 2008 8:32:19 PM

Rank: Fanatic

Joined: 7/20/2006
Posts: 453
Location: NL
Glad you got it working :D

PeterD

Working on an events-calendar with recursion. Post requests on my blog!
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.