Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Sitecore Extensions Methods

RSS
Modified on Thu, Apr 19, 2012, 8:22 AM by Administrator Categorized as ASP·NET Web Forms, Sitecore
public static class SitecoreExtensions
{
    public static string TweakItemName(this string name, string replaceWith = "-")
    {
        name = name.Replace("&", "And");
        name = name.Replace("+", "Plus");

        var invalidChars = Settings.InvalidItemNameChars;

        foreach (var c in invalidChars)
            name = name.Replace(c.ToString(), replaceWith);

        name = name.Trim();
        return name;
    }

    /// <param name="itemId">The GUID for the item, as a string, with or without braces.</param>
    public static Sitecore.Data.Items.Item GetItemFromID(this Sitecore.Data.Database db, string itemId)
    {
        if (!itemId.StartsWith("{"))
            itemId = "{" + itemId;

        if (!itemId.EndsWith("}"))
            itemId += "}";

        return db.GetItem(new Sitecore.Data.ID(itemId));
    }


    /// <param name="itemId">The GUID for the template, as a string, with or without braces.</param>
    public static Sitecore.Data.Items.TemplateItem GetTemplateFromID(this Sitecore.Data.Database db, string itemId)
    {
        if (!itemId.StartsWith("{"))
            itemId = "{" + itemId;

        if (!itemId.EndsWith("}"))
            itemId += "}";

        return db.Templates[new Sitecore.Data.ID(itemId)];
    }



    public static string GetField(this Item item, string fieldName)
    {
        var fld = item.Fields[fieldName];

        if (fld == null)
            return "";

        return fld.Value;
    }

    /// <param name="fieldName">The name of the INT field</param>
    /// <param name="addend">The amount to add to the current value of the INT field</param>
    public static void MathAdd(this Item item, string fieldName, int addend)
    {
        try
        {
            var s = item.GetField(fieldName);
            int val;

            if (!int.TryParse(s, out val))
                val = 0;

            val += addend;

            item[fieldName] = val.ToString();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public static MediaItem GetMediaItem(this Item item, string fieldName)
    {
        FileField fld = item.Fields[fieldName];

        if (fld == null)
            return null;

        return (MediaItem)fld.MediaItem;
    }

    public static string DownloadUrl(this MediaItem m)
    {
        if (m == null) return "";
        //return string.Format("/~/media{0}.{1}", m.MediaPath, m.Extension);
        //return string.Format(Helper.AppRootUrl + "Media{0}.{1}", m.MediaPath, m.Extension);
        var s = StringUtil.EnsurePrefix('/', MediaManager.GetMediaUrl(m));
        return s;
    }

    public static void EnsureAllLanguages(this Item item)
    {
        foreach (var language in item.Languages)
        {
            var langItem = item.Database.GetItem(item.ID, language);

            if (langItem.Versions.Count < 1)
                langItem = langItem.Versions.AddVersion();
        }
    }

    public static string CurrentPageUrl(bool includeQueryString = false)
    {
        var result = "";
        var r = HttpContext.Current.Request;
        result += r.Url.Scheme;                 // "http" or "https"
        result += Uri.SchemeDelimiter;          // "://"
        result += Sitecore.Context.Site.HostName;
        result += Sitecore.Context.RawUrl;

        if (!includeQueryString & result.Contains("?"))
        {
            var pos = result.LastIndexOf("?");
            result = result.Substring(0, pos);
        }

        return result;
    }

    public static string ToSitecoreID(this Guid guid)
    {
        return "{" + guid.ToString().ToUpperInvariant() + "}";
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.