Wednesday, November 11, 2009

Adding Control to Module & Data Fetch

If you would like to add the key value. in any inbuilt sitefinity module
say for example. active check box on the module. while fetching a value based on the check pull the data.

1)


2) while fetching the data to your userControl

Namespace
using Telerik.Cms;
using Telerik.Newsletters;
using Telerik.Notifications.Newsletters;
using System.Collections;
using Telerik.Cms.Engine;
using Telerik.Cms.Web;



3).cs

void GetAllNews()
{

NewsManager objnewsManager = new NewsManager("News");
IMetaSearchInfo[] filters = new IMetaSearchInfo[4];
filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);
filters[1] = new MetaSearchInfo(MetaValueTypes.ShortText, "Category", ConfigurationManager.AppSettings["NewsandPressrelease"].ToString());
filters[2] = new MetaSearchInfo(MetaValueTypes.Boolean, "Active",true);
filters[3] = new MetaSearchInfo(MetaValueTypes.Boolean, "English", false);
IList lstListOfNewsItems = objnewsManager.Content.GetContent("Publication_Date DESC", filters);
DataTable dtNewsValue = new DataTable();
dtNewsValue.Columns.Add("MoreURL");
dtNewsValue.Columns.Add("Publication_Date");
dtNewsValue.Columns.Add("Title");
dtNewsValue.Columns.Add("Thumbnail");
int icnt = 0;
QFCLib objQFCLib = new QFCLib();

try
{

if (lstListOfNewsItems.Count > 0)
{
foreach (IContent ICntNews in lstListOfNewsItems)
{
string strImageURL = "";

if (ICntNews.GetMetaData("Thumbnail").ToString() != "" && ICntNews.GetMetaData("Thumbnail").ToString() != null)
{
strImageURL = objQFCLib.GetThumbnailURL(ICntNews.GetMetaData("Thumbnail").ToString()) + "?width=87&height=79&proportional=false&decreaseOnly=true";
}
DateTime strPDate = Convert.ToDateTime(ICntNews.GetMetaData("Publication_Date"));
if (strPDate < DateTime.Now)
{
icnt = icnt + 1;
dtNewsValue.Rows.Add(ConfigurationManager.AppSettings["NewsDetails"].ToString() + "?sNewsID=" + ICntNews.ID, strPDate.ToString("MM.dd.yyyy"), ICntNews.GetMetaData("Title"), strImageURL);

}
if (icnt == 5) break;
}
}

if (dtNewsValue.Rows.Count > 0)
{
repeater.DataSource = dtNewsValue;
repeater.DataBind();
dtNewsValue.Dispose();

}
else
{
lblMsg.Text = Resources.ARCommonResource.NoDataFound;
}
}
catch (Exception ex)
{
AppLogger.LogError("GetAllNews Mediacenter() " + ex.Message, AppLogger.Severity.Error);
}

}

4) protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlGenericControl newsThumbnailImgContainer = (HtmlGenericControl)e.Item.FindControl("newsThumbnailImgContainer");

if (((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[3].ToString() != "")
{
newsThumbnailImgContainer.Visible = true;
}
}
}
catch (Exception ex)
{
AppLogger.LogError("DataBound MediaArchive() " + ex.Message, AppLogger.Severity.Error);
}

}
5)if the content level if any images to get the image
Add to the cs file
#region " Sitefinity Method GetItemUrl "
public string GetItemUrl(string provider, Guid id, bool resolveAsAbsoluteUrl)
{
if (ContentManager.Providers.ContainsKey(provider))
{
IContent cnt = ContentManager.Providers[provider].GetContent(id);
if (cnt != null)
return VirtualPathUtility.ToAbsolute(cnt.UrlWithExtension, HttpContext.Current.Request.ApplicationPath);
}
else
{
ICmsUrlContext urlContext = UrlHelper.GetUrl(id);
if (urlContext != null)
{
SiteMapNode node = urlContext.GetSiteMapNode();
if (node != null)
return VirtualPathUtility.ToAbsolute(node.Url,HttpContext.Current.Request.ApplicationPath);
}
}
return String.Concat("Item not found: [", provider, "]", id);
}
#endregion

#region " Get Thumbnail URL "
public string GetThumbnailURL(string pstrThumbnail)
{

string[] strImage = pstrThumbnail.Split(']');
string strImageURL=string.Empty;
if (strImage.Length == 2)
{
Guid idImage = new Guid(strImage[1]);
strImageURL = GetItemUrl(strImage[0].Substring(1).ToString(), idImage, true);
}
else
{
return pstrThumbnail.Replace("~","");
}
return strImageURL;
}
#endregion
strImageURL = objQFCLib.GetThumbnailURL(ICntNews.GetMetaData("Thumbnail").ToString()) + "?width=87&height=79&proportional=false&decreaseOnly=true";


6) Upload the above ascx on the page leve add controls via and drag.

Copyright © 2009 Angel