1. Problem: Getting Sitecore Multilist items which has broken links
Solution:
MultilistField multilistField = Sitecore.Context.Item.Fields["your_field_name"];
if (multilistField != null)
{
var items = multilistField.TargetIDs.Select(id => Sitecore.Context.Database.GetItem(id)).Where(id => id != null);
//use items
}
2. Dynamically assign images to the sitecore items from media library
private void AssignImageFromMediaLibrary(string sitecoreImagepath, Item itm, string fieldName)
{
string error = string.Empty;
try
{
Sitecore.Data.Fields.ImageField imageField = itm.Fields[fieldName];
if (!String.IsNullOrEmpty(sitecoreImagepath))
{
sitecoreImagepath= sitecoreImagepath.Replace(".jpg", "");
sitecoreImagepath= sitecoreImagepath.Replace(".JPG", "");
sitecoreImagepath= sitecoreImagepath.Replace(".png", "");
sitecoreImagepath= sitecoreImagepath.Replace(".gif", "");
sitecoreImagepath= sitecoreImagepath.Replace("%20", " ");
error = sitecoreImagepath;
Sitecore.Data.Items.MediaItem medialImage = new Sitecore.Data.Items.MediaItem(currentDB.GetItem(sitecoreImagepath));
imageField.Alt = medialImage.Alt;
imageField.MediaID = medialImage.ID;
imageField.Src = MediaManager.GetMediaUrl(medialImage);
imageField.MediaPath = medialImage.MediaPath;
}
else
{
imageField.Src = "";
imageField.MediaPath = "";
}
}
catch (Exception ex)
{
lblerror.Text = "Error:-- " + error;
lblerror.ForeColor = System.Drawing.Color.Red;
}
}
3. Creating language version to sitecore item
private Item CreateVersion(Item itm string languageCode)
{
Item destinationLanguageItem = currentDB.GetItem(itm.ID, Sitecore.Globalization.Language.Parse("languageCode)")); //Language code example : de-DE
destinationLanguageItem.Versions.AddVersion();
return destinationLanguageItem;
}
4. How to select sitecore items in mulitlist from code behind
private void SelectMultiListItemColletions(string itemIDs, Item itm, string fieldName)
{
MultilistField appMultilist = itm.Fields[fieldName];
appMultilist.Value = itemIDs;//itemIDs are sapeated with pipe symbol "|"
}
5. Sitecore Fallback Language
Private void FallbackLanguage(string langCode)
{
Sitecore.Globalization.Language fallbackLang = Sitecore.Globalization.Language.Parse("en-EN");
Sitecore.Globalization.Language setLang = Sitecore.Globalization.Language.Parse(langCode));
Item contextItem = Sitecore.Context.Item;
LanguageCollection collection = ItemManager.GetContentLanguages(contextItem);
bool isLanguageExist = false;
for (int i = 0; i < collection.Count; i++)
{
var itm = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, collection[i]);
if (setLang == collection[i])
{
if (itm.Versions.Count > 0)
{
isLanguageExist = true;
}
}
}
if (isLanguageExist)
{
Sitecore.Context.SetLanguage(setLang, true);
}
else
{
Sitecore.Context.SetLanguage(fallbackLang, true);
}
Response.Redirect(Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item));
}
Solution:
MultilistField multilistField = Sitecore.Context.Item.Fields["your_field_name"];
if (multilistField != null)
{
var items = multilistField.TargetIDs.Select(id => Sitecore.Context.Database.GetItem(id)).Where(id => id != null);
//use items
}
2. Dynamically assign images to the sitecore items from media library
private void AssignImageFromMediaLibrary(string sitecoreImagepath, Item itm, string fieldName)
{
string error = string.Empty;
try
{
Sitecore.Data.Fields.ImageField imageField = itm.Fields[fieldName];
if (!String.IsNullOrEmpty(sitecoreImagepath))
{
sitecoreImagepath= sitecoreImagepath.Replace(".jpg", "");
sitecoreImagepath= sitecoreImagepath.Replace(".JPG", "");
sitecoreImagepath= sitecoreImagepath.Replace(".png", "");
sitecoreImagepath= sitecoreImagepath.Replace(".gif", "");
sitecoreImagepath= sitecoreImagepath.Replace("%20", " ");
error = sitecoreImagepath;
Sitecore.Data.Items.MediaItem medialImage = new Sitecore.Data.Items.MediaItem(currentDB.GetItem(sitecoreImagepath));
imageField.Alt = medialImage.Alt;
imageField.MediaID = medialImage.ID;
imageField.Src = MediaManager.GetMediaUrl(medialImage);
imageField.MediaPath = medialImage.MediaPath;
}
else
{
imageField.Src = "";
imageField.MediaPath = "";
}
}
catch (Exception ex)
{
lblerror.Text = "Error:-- " + error;
lblerror.ForeColor = System.Drawing.Color.Red;
}
}
3. Creating language version to sitecore item
private Item CreateVersion(Item itm string languageCode)
{
Item destinationLanguageItem = currentDB.GetItem(itm.ID, Sitecore.Globalization.Language.Parse("languageCode)")); //Language code example : de-DE
destinationLanguageItem.Versions.AddVersion();
return destinationLanguageItem;
}
4. How to select sitecore items in mulitlist from code behind
private void SelectMultiListItemColletions(string itemIDs, Item itm, string fieldName)
{
MultilistField appMultilist = itm.Fields[fieldName];
appMultilist.Value = itemIDs;//itemIDs are sapeated with pipe symbol "|"
}
5. Sitecore Fallback Language
Private void FallbackLanguage(string langCode)
{
Sitecore.Globalization.Language fallbackLang = Sitecore.Globalization.Language.Parse("en-EN");
Sitecore.Globalization.Language setLang = Sitecore.Globalization.Language.Parse(langCode));
Item contextItem = Sitecore.Context.Item;
LanguageCollection collection = ItemManager.GetContentLanguages(contextItem);
bool isLanguageExist = false;
for (int i = 0; i < collection.Count; i++)
{
var itm = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, collection[i]);
if (setLang == collection[i])
{
if (itm.Versions.Count > 0)
{
isLanguageExist = true;
}
}
}
if (isLanguageExist)
{
Sitecore.Context.SetLanguage(setLang, true);
}
else
{
Sitecore.Context.SetLanguage(fallbackLang, true);
}
Response.Redirect(Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item));
}
No comments:
Post a Comment