Sitecore admin script pages
Sometimes you need a little sitecore script to get something done real quick. If you’ve got it installed, you could reach for Sitecore powershell, and just use that, but I find the syntax obtuse and the lack of intellisense support to be limiting.
Usually what I do is just toss what I call a script page into the admin section, and use it.
Here’s an example to get you started. I needed to run through all the pages in a folder and set a field to a certain value.
It’s hardcoded here, but you could add <asp:TextBox>
fields as needed to make
it a little bit more generic.
Just drop the page in the /sitecore/admin
folder of your local or development
server.
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import namespace="Sitecore.Data" %>
<%@ Import namespace="Sitecore.Data.Items" %>
<script runat="server" language="C#">
void MyButton_OnClick(Object sender, EventArgs e)
{
Database db = Database.GetDatabase("master");
int count = 0;
Item parent = db.GetItem(path.Text);
foreach (Item child in parent.GetChildren())
{
child.Editing.BeginEdit();
child["SomeLookupField"] = "{8312B639-4463-44AB-84D9-DD021FF78F01}";
child.Editing.EndEdit();
count++;
}
Output.Text = count + " items updated";
}
</script>
<html>
<head runat="server">
<title>Magic Field Updater Script</title>
<link rel="shortcut icon" href="/sitecore/images/favicon.ico" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Textbox id="path" runat="server" placeholder="/sitecore/item/somesite/a-folder/2004" width="500" />
</div>
<div>
<asp:Button id="c_reset" runat="server" OnClick="MyButton_OnClick" Text="Update SomeLookupField" />
</div>
<asp:Literal id="Output" runat="server"></asp:Literal>
<p>Enter a path to a year folder to set all the childrens SomeLookupField value.</p>
</form>
</body>
</html>
If you wanted to get really fancy, you could add more logging through a StringBuilder or something. But this is usually good enough for scripting work.
If you need a lot of UI, there are ways to customize the Sitecore ContentEditor application to add your own dialogs, buttons, etc.
It should go without saying, but don’t deploy these to your production sites.
I’m not sure if Sitecore protects everything in /sitecore/admin
by default or
not. Six months from now, I better not be able to google for “Magic Field
Updater Script” and find anything but this post.