Hi,
I'm a new in SharePoint,
I got a task to improve the code, it's not a good code. Too much if/else condition, string concatenation and most difficult part is the complicated logic with SPListItemCollection, I gonna break it down by parts and trying to apply Single Responsibility at least and I didn't find a way yet, but the first I need to improve a performance of the code
Give any idea, i really need to get help or suggestions. (Sorry for typos, English is not my first language)
Thanks in advance.
protected void Page_Load(object sender, EventArgs e)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
Guid webid = SPContext.Current.Web.ID;
int lcid = (int)SPContext.Current.Web.Language;
string newsweb = "en/News";
string Pagelibrary = "/en/News/Pages";
string sitename = "en";
Utility utl = new Utility();
string LangLcid = utl.GetCurrentLanguageUI();
string newstitle = utl.GetLabelName("LandingpageNews", LangLcid);
string readmoretext = utl.GetLabelName("NewsReadmore", LangLcid);
string viewallnewstext = utl.GetLabelName("NewsViewall", LangLcid);
if (lcid == 1025)
{
newsweb = "ar/News";
Pagelibrary = "/ar/News/Pages";
sitename = "ar";
}
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb(newsweb);
using (SPSite site = new SPSite(site.ID))
{
SPList list = null;
using (SPWeb web = site.OpenWeb(newsweb))
{
list = web.GetList(Pagelibrary);
}
SPQuery query = new SPQuery();
System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
DateTime time = DateTime.Now;
string format = "yyyy-MM-dd";
string comparedate = time.ToString(format);
if (lcid == 1025)
{
int date = time.Day;
string datestring = string.Empty;
string monthstring = string.Empty;
int month = time.Month;
int year = time.Year;
if (date < 10)
{
datestring = "0" + date.ToString();
}
else
{
datestring = date.ToString();
}
if (month < 10)
{
monthstring = "0" + month.ToString();
}
else
{
monthstring = month.ToString();
}
comparedate = year.ToString() + "-" + monthstring + "-" + datestring;
}
query.Query = @"
<OrderBy>
<FieldRef Name='NewsArticleStartDate' Ascending='FALSE' />
</OrderBy>
<Where>
<And>
<Eq>
<FieldRef Name='_ModerationStatus' />
<Value Type='ModStat'>0</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='Priority' />
<Value Type='Text'>Yes</Value>
</Eq>
<Leq>
<FieldRef Name='NewsArticleStartDate' />
<Value IncludeTimeValue='TRUE' Type='DateTime'>" + comparedate + @"</Value>
</Leq>
</And>
</And>
</Where>";
SPListItemCollection itemcol = list.GetItems(query);
if (itemcol.Count > 0)
{
string viewalllink = string.Empty;
viewalllink = "<a class='moewnews' href='/en/News/Pages/Newshome.aspx?currentpage=1'>" + viewallnewstext + "<img src='/_catalogs/masterpage/img/arrow-news.png'></a>" + newstitle;
if (lcidcheck == 1025)
viewalllink = "<a class='moewnews' href='/ar/News/Pages/Newshome.aspx?currentpage=1'>" + viewallnewstext + "<img src='/_catalogs/masterpage/img/icon_arrow-right.png'></a>" + newstitle;
title.InnerHtml = viewalllink;
string publishingRollupImageField = itemcol[0].GetFormattedValue("NewsPageImage");
string pageImageurl;
if (publishingRollupImageField != string.Empty)
{
profimg1.InnerHtml = publishingRollupImageField;
}
else
{
profimg1.InnerHtml = "<img src='/_catalogs/masterpage/img/conf.jpg'/>";
}
string titleurl = @"<a href='/" + sitename + "/News/" + itemcol[0].Url.ToString() + "'>" + itemcol[0]["Title"].ToString() + "</a>";
firsttitle.InnerHtml = titleurl;
firstnav.InnerHtml = @" <a href='#tab_a' data-toggle='pill'>" + itemcol[0]["Title"].ToString() + "</a>";
if (itemcol[0].GetFormattedValue("PublishingPageContent").Length <= 150)
{
firstinfo.InnerText = HtmlRemoval.StripTagsRegex(itemcol[0].GetFormattedValue("PublishingPageContent"));
}
else
{
string temstring = HtmlRemoval.StripTagsRegex(itemcol[0].GetFormattedValue("PublishingPageContent"));
int tempindex = 149;
try
{
do
{
tempindex++;
} while (temstring[tempindex] != ' ');
}
catch (Exception ex)
{
}
firstinfo.InnerText = HtmlRemoval.StripTagsRegex(itemcol[0].GetFormattedValue("PublishingPageContent")).Substring(0, tempindex) + "...";
}
string articledate = itemcol[0]["NewsArticleStartDate"].ToString();
DateTime date = Convert.ToDateTime(articledate);
string firstdatetext = String.Format("{0:m}", date) + ", " + date.Year;
firstdate.InnerText = firstdatetext;
secondtitile.InnerHtml = @"<a class='firsthead' href='/" + sitename + "/News/" + itemcol[1].Url.ToString() + "'>" + itemcol[1]["Title"].ToString() + "</a>";
secondnav.InnerHtml = @" <a href='#tab_b' data-toggle='pill'>" + itemcol[1]["Title"].ToString() + "</a>";
string secondarticledate = itemcol[1]["NewsArticleStartDate"].ToString();
if (itemcol[1].GetFormattedValue("NewsPageImage") != null)
{
string publishingRollupImageFieldsecond = itemcol[1].GetFormattedValue("NewsPageImage");
if (publishingRollupImageFieldsecond != string.Empty)
{
profimg2.InnerHtml = publishingRollupImageFieldsecond;
}
else
{
profimg2.InnerHtml = "<img src='/_catalogs/masterpage/img/conf.jpg'/>";
}
}
if (itemcol[1].GetFormattedValue("PublishingPageContent") != null)
{
if (itemcol[1].GetFormattedValue("PublishingPageContent").Length <= 150)
{
secondinfo.InnerText = HtmlRemoval.StripTagsRegex(itemcol[1].GetFormattedValue("PublishingPageContent"));
}
else
{
string temstring = HtmlRemoval.StripTagsRegex(itemcol[1].GetFormattedValue("PublishingPageContent"));
int tempindex = 149;
try
{
do
{
tempindex++;
} while (temstring[tempindex] != ' ');
}
catch (Exception ex)
{
}
secondinfo.InnerText = HtmlRemoval.StripTagsRegex(itemcol[1].GetFormattedValue("PublishingPageContent")).Substring(0, tempindex) + "...";
}
}
DateTime seconddatee = Convert.ToDateTime(secondarticledate);
string seconddatetext = String.Format("{0:m}", seconddatee) + ", " + seconddatee.Year;
seconddate.InnerText = seconddatetext;
thirdtitle.InnerHtml = @"<a class='secondhead' href='/" + sitename + "/News/" + itemcol[2].Url.ToString() + "'>" + itemcol[2]["Title"].ToString() + "</a>";
thirdnav.InnerHtml = @" <a href='#tab_c' data-toggle='pill'>" + itemcol[2]["Title"].ToString() + "</a>";
string thirdarticledate = itemcol[2]["NewsArticleStartDate"].ToString();
DateTime thirddatee = Convert.ToDateTime(thirdarticledate);
string thirddatetext = String.Format("{0:m}", thirddatee) + ", " + thirddatee.Year;
thirddate.InnerText = thirddatetext;
if (itemcol[2].GetFormattedValue("NewsPageImage") != null)
{
string publishingRollupImageFieldthird = itemcol[2].GetFormattedValue("NewsPageImage");
if (publishingRollupImageFieldthird != string.Empty)
{
profimg3.InnerHtml = publishingRollupImageFieldthird;
}
else
{
profimg3.InnerHtml = "<img src='/_catalogs/masterpage/img/conf.jpg'/>";
}
}
if (itemcol[2].GetFormattedValue("PublishingPageContent") != null)
{
if (itemcol[2].GetFormattedValue("PublishingPageContent").Length <= 150)
{
thirdinfo.InnerText = HtmlRemoval.StripTagsRegex(itemcol[2].GetFormattedValue("PublishingPageContent"));
}
else
{
string temstring = HtmlRemoval.StripTagsRegex(itemcol[2].GetFormattedValue("PublishingPageContent"));
int tempindex = 149;
try
{
do
{
tempindex++;
} while (temstring[tempindex] != ' ');
}
catch (Exception ex)
{
}
thirdinfo.InnerText = HtmlRemoval.StripTagsRegex(itemcol[2].GetFormattedValue("PublishingPageContent")).Substring(0, tempindex) + "...";
}
}
}
}
});
}
catch (Exception ex)
{
}
}
What I have tried:
What i going to do:
1) String.Formate or StringBuilder instead of concatenation
2) Sets a limit for the items being returned in your query.
3) Include only the fields i will use. ViewFields: it helps on defining which fields are going to be returned in query.
4) I think to use Caching Data and Objects it is one good technique to improve performance.