views:

200

answers:

2

Hello, I am trying to add an image item to the content tree - but I get an access denied error at the following point: item.Add("New Node1"...

The complete code of my method is: Sitecore.Data.Database master; master = Sitecore.Configuration.Factory.GetDatabase("master"); Item item = master.Items["/sitecore/Content/Media/Images/Places"]; // item.Fields["Related Issues"].Value = "Asia and the Pacific"; if (item != null) { // add a new Item based on the Document Template Item itm = item.Add("New Node1", master.Templates[new ID(new Guid("EJ0F53DF-5486-4UF4-A2D1-64C119E419A5"))]); if (itm != null) { // report the Item path to the User Response.Write(itm.Paths.Path); } }

+1  A: 
    Here is the code sample

 Sitecore.Data.Database master;
            master = Sitecore.Configuration.Factory.GetDatabase("master");
            Item item = master.Items["/sitecore/Content/Media/Images/Places"];          
            if (item != null)
            {                   
                Item itm = 
 item.Add("New Node1", master.Templates[new ID(new Guid("EF0F53DF-5486-49F4-A2D1-64C119E419A5"))]);
                if (itm != null)
                {
                    // report the Item path to the User
                    Response.Write(itm.Paths.Path);
                }
            }
+2  A: 

It is possible that the context of the user Sitecore is running in does not have permissions to this part of the content tree. Check this using the Access Viewer tool. If this is the case then wrap the method in the Security Disabler, like so:

using (new SiteCore.SecurityModel.SecurityDisabler())
{
  // enter your code here
}
Michael Edwards