Sitecore Item Web API Client: Create

This is a quick update to announce that the client library now supports item creation operations. There are a couple of considerations that need to be taken into account with regards to Security

  1. You must update the itemwebapi.access attribute for the target site in Sitecore.ItemWebApi.config to ReadWrite
  2. You must use an AuthenticatedSitecoreDataContext
  3. The user must have create permissions on the parent item
  4. To update the newly created items fields, the user must have write permissions for the item

Warning!

When creating new items you can define the parent item using Sitecore query, to be sure where your new item will be created limit the scope of the query to match a single item e.g. avoid things such as descendant selectors.

Example

var credentials = new SitecoreCredentials();

credentials.UserName = "sitecore\\foo";
credentials.Password = "bar";

var context = new AuthenticatedSitecoreDataContext("host", credentials);

var query = new SitecoreCreateQuery();

// the name of the new item
query.Name = "Foo";

// the id of the parent item
query.ItemId = "{11111111-1111-1111-1111-111111111111}";

// alternatively specify the parent with a query
// if you specify the ItemId it will take precedence
query.ParentQuery = "/sitecore/content/Home";

// Template can be a template id, branch id or path relative to the
// sitecore/Templates folder
query.Template = "{11111111-1111-1111-1111-111111111111}";
query.Database = "master";

query.FieldsToUpdate = new Dictionary<string, string>();

// fields can be specified by name or id
query.FieldsToUpdate.Add("Field Name", "Value");
query.FieldsToUpdate.Add("{11111111-1111-1111-1111-111111111111}", "Value");

// only return the fields we updated in the response to validate the operation
query.FieldsToReturn = new List<string>();
query.FieldsToReturn.Add("Field Name");
query.FieldsToReturn.Add("{11111111-1111-1111-1111-111111111111}");

ISitecoreWebResponse response = context.GetResponse(query);

if (response.StatusCode == HttpStatusCode.OK)
{
    Console.WriteLine(response.Result.Count);
    
    foreach (WebApiItem item in response.Result.Items)
    {
        Console.WriteLine(item.Path);
    }
}

Download the code here

Advertisements

Sitecore Item Web API Client: Update

This is a quick update to announce that the client library now supports field update operations. There are a couple of considerations that need to be taken into account with regards to Security

  1. You must update the itemwebapi.access attribute for the target site in Sitecore.ItemWebApi.config to ReadWrite
  2. You must use an AuthenticatedSitecoreDataContext
  3. The user must have write permissions on the item

Warning!

Field update queries are affected by the scope parameter. If the query is scoped to the parent or children of the item that matches the query or item id the fields on the parent and child items will also be updated.

Example

Update queries can be used with either item or expression queries. In the case of expression queries, all items matching the query will be updated.

var credentials = new SitecoreCredentials();

credentials.UserName = "sitecore\\foo";
credentials.Password = "bar";

var context = new AuthenticatedSitecoreDataContext("host", credentials);

var query = new SitecoreItemQuery();

// the id of the item to update
query.ItemId = "{11111111-1111-1111-1111-111111111111}";

query.Database = "master";

query.FieldsToUpdate = new Dictionary<string, string>();

// fields can be specified by name or id
query.FieldsToUpdate.Add("Field Name", "Value");
query.FieldsToUpdate.Add("{11111111-1111-1111-1111-111111111111}", "Value");

// only return the fields we updated in the response to validate the operation
query.FieldsToReturn = new List();
query.FieldsToReturn.Add("Field Name");
query.FieldsToReturn.Add("{11111111-1111-1111-1111-111111111111}");

ISitecoreWebResponse response = context.GetResponse(query);

if (response.StatusCode == HttpStatusCode.OK)
{
    Console.WriteLine(response.Result.Count);

    foreach (WebApiItem item in response.Result.Items)
    {
        Console.WriteLine(item.Path);
    }
}

Download the code here

Sitecore Item Web API Client: Delete

This is a quick update to announce that the client library now supports delete operations. There are a couple of considerations that need to be taken into account with regards to Security

  1. You must update the itemwebapi.access attribute for the target site in Sitecore.ItemWebApi.config to ReadWrite
  2. You must use an AuthenticatedSitecoreDataContext
  3. The user must have delete permissions in order to delete items. The easiest way to check if this is the case is to use Sitecore’s Security Access Viewer

Warning!

Deleting items is permanent. Deleted items do not go into the recycle bin so please use with caution. Also, the Sitecore Item Web API returns a count of the number of items that were deleted as a result of the query, this count shows you the total number of items that were matched by the query and successfully deleted, it does not also include a count of the descendant items that may have gone with them.

Example

var credentials = new SitecoreCredentials();

credentials.UserName = "sitecore\\foo";
credentials.Password = "bar";

var context = new AuthenticatedSitecoreDataContext("host", credentials);

var query = new SitecoreItemQuery(SitecoreQueryType.Delete);

query.ItemId = "{11111111-1111-1111-1111-111111111111}";

// a safeguard, limit the scope of the deletion
query.QueryScope = new[] { SitecoreItemScope.Self };

// another safeguard, items can always be re-published
query.Database = "web";

ISitecoreWebResponse response = context.GetResponse(query);

if (response.StatusCode == HttpStatusCode.OK)
{
    Console.WriteLine(response.Result.Count);

    // the ids of the items deleted (does not include descendants)
    if (response.Result.ItemIds != null)
    {
        foreach (string id in response.Result.ItemIds)
        {
            Console.WriteLine(id);
        }
    }
}

Download the code here

Sitecore Icon Selector

As Sitecore developers we must never take our mind off the content author experience when building a Sitecore solution. Sitecore itself has a community section dedicated to this very topic.

One of the ways we can do this and mentioned in the post 6 ways to improve your content author experience is to choose unique icons per template to provide content authors with visual clues to the purpose of content items.

Not only should we be providing visual points of reference in the content tree but also in content sections

Unfortunately the process of choosing icons can become a bit of a slow and tedious task. Icon Selector allows developers to automatically assign icons to templates and template sections when they are created by defining patterns using the Sitecore Rules Engine.

For more information see the full blog post at non~linear creations

Or to download the module or the source code go to http://trac.sitecore.net/IconSelector

Happy Coding!