帮助或建议以提高性能 - sharepoint [英] Help or suggestions to improve performance - sharepoint
问题描述
我是SharePoint的新手,
我有一个任务改进代码,这不是一个好的代码。太多的if / else条件,字符串连接和最困难的部分是SPListItemCollection的复杂逻辑,我将按部分分解并尝试至少应用单一责任而我还没找到方法,但我需要的第一个提高代码的性能
任何想法,我真的需要得到帮助或建议。 (抱歉打字错误,英语不是我的第一语言)
提前致谢。
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))
{
//listGuid = web.GetList(Pagelibrary).ID.ToString();
list = web.GetList(Pagelibrary);
}
SPQuery query = new SPQuery();
System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
DateTime time = DateTime.Now; // Use current time
string format = "yyyy-MM-dd"; // Use this format
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)
{
}
}
我尝试过的事情:
我打算怎样执行:
1)String.Formate或StringBuilder而不是连接
2)为查询中返回的项设置限制。
3)仅包括我将使用的字段。 ViewFields:它有助于定义将在查询中返回哪些字段。
4)我认为使用缓存数据和对象这是一种提高性能的好方法。
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.
推荐答案
就我个人而言,我会把它全部扔掉然后重新开始,但如果要保留它,请将代码分解成小块并将它们放入方法中以便开始。读取所有意大利面条代码(特别是在加载事件中)是痛苦的。我想你最好重新开始。
Personally I'd throw it all away and start again but if you are going to keep it, break the code down into little chunks and put them in methods for a start. It's painful reading all that spaghetti code ( especially in a load event ). I think you will be better off starting afresh.
这篇关于帮助或建议以提高性能 - sharepoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!