我需要在这里处理SPWeb吗...?
protected void getNews()
{
using (SPWeb web = getWeb("InternalNews"))
{
fetchNewsFromWeb(ref dtNews,true,"English",new string[] { "Internal news page" },web,startDate,endDate,false,true);
}
}
protected SPWeb getWeb(string contentTypeUrlKey)
{
try
{
List<string> urls = CTUrlWrapper.GetContentTypeUrl(contentTypeUrlKey, this.Page.Request.Url.ToString());
return SPContext.Current.Site.OpenWeb(urls[0].ToLowerInvariant().Replace(SPContext.Current.Site.Url.ToLowerInvariant(), "").TrimStart('/'));
}
catch
{
throw new Exception("Can not fetch value from CTUrl list, key: \"" + contentTypeUrlKey + "\"");
}
}
我真的需要在 fetchNewsFromWeb 方法中处理网络吗?
protected DataTable fetchNewsFromWeb(ref DataTable dtAllData, bool useCriticalField, string pageLanguage, string[] contentTypes, SPWeb web, DateTime? fromDate, DateTime? toDate, bool otherUnitNews, bool useHeaderPrefix)
{
SPSiteDataQuery sdq = GetQuery(useCriticalField);
StringBuilder sbQuery = new StringBuilder();
sbQuery.Append(getWhereClause(pageLanguage, fromDate, toDate, contentTypes, otherUnitNews));
sbQuery.Append(getOrderByClause(useCriticalField));
sdq.Query = sbQuery.ToString();
try
{
DataTable foundItems = web.GetSiteData(sdq);
if (foundItems.Rows.Count > 0)
{
foreach (DataRow row in foundItems.Rows)
{
try
{
object[] dtAlldataTemp = extractNewsFields(row, useHeaderPrefix);
dtAllData.Rows.Add(dtAlldataTemp);
}
catch (Exception ex)
{
}
}
}
}
catch (Exception ex)
{
}
finally {web.Dispose();}// do we really need this here?
return dtAllData;
}
protected SPWeb getWeb(string contentTypeUrlKey)
{
try
{
List<string> urls = CTUrlWrapper.GetContentTypeUrl(contentTypeUrlKey, this.Page.Request.Url.ToString());
return SPContext.Current.Site.OpenWeb(urls[0].ToLowerInvariant().Replace(SPContext.Current.Site.Url.ToLowerInvariant(), "").TrimStart('/'));
}
catch
{
throw new Exception("Can not fetch value from CTUrl list, key: \"" + contentTypeUrlKey + "\"");
}
}
protected void getNews()
{
using (SPWeb web = getWeb("InternalNews"))
{
fetchNewsFromWeb(ref dtNews,true,"English",new string[] { "Internal news page" },web,startDate,endDate,false,true);
}
}
protected SPWeb getWeb(string contentTypeUrlKey)
{
try
{
List<string> urls = CTUrlWrapper.GetContentTypeUrl(contentTypeUrlKey, this.Page.Request.Url.ToString());
return SPContext.Current.Site.OpenWeb(urls[0].ToLowerInvariant().Replace(SPContext.Current.Site.Url.ToLowerInvariant(), "").TrimStart('/'));
}
catch
{
throw new Exception("Can not fetch value from CTUrl list, key: \"" + contentTypeUrlKey + "\"");
}
}
Do I really need to dispose the web in fetchNewsFromWeb method?
protected DataTable fetchNewsFromWeb(ref DataTable dtAllData, bool useCriticalField, string pageLanguage, string[] contentTypes, SPWeb web, DateTime? fromDate, DateTime? toDate, bool otherUnitNews, bool useHeaderPrefix)
{
SPSiteDataQuery sdq = GetQuery(useCriticalField);
StringBuilder sbQuery = new StringBuilder();
sbQuery.Append(getWhereClause(pageLanguage, fromDate, toDate, contentTypes, otherUnitNews));
sbQuery.Append(getOrderByClause(useCriticalField));
sdq.Query = sbQuery.ToString();
try
{
DataTable foundItems = web.GetSiteData(sdq);
if (foundItems.Rows.Count > 0)
{
foreach (DataRow row in foundItems.Rows)
{
try
{
object[] dtAlldataTemp = extractNewsFields(row, useHeaderPrefix);
dtAllData.Rows.Add(dtAlldataTemp);
}
catch (Exception ex)
{
}
}
}
}
catch (Exception ex)
{
}
finally {web.Dispose();}// do we really need this here?
return dtAllData;
}
protected SPWeb getWeb(string contentTypeUrlKey)
{
try
{
List<string> urls = CTUrlWrapper.GetContentTypeUrl(contentTypeUrlKey, this.Page.Request.Url.ToString());
return SPContext.Current.Site.OpenWeb(urls[0].ToLowerInvariant().Replace(SPContext.Current.Site.Url.ToLowerInvariant(), "").TrimStart('/'));
}
catch
{
throw new Exception("Can not fetch value from CTUrl list, key: \"" + contentTypeUrlKey + "\"");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,你不知道。
using
将负责处理它。No you don't.
The
using
will take care of disposing it already.您是否询问过SPDisposeCheck?
Did you ask SPDisposeCheck?
这取决于 getWeb 函数如何返回 SPWeb。
如果它只是一个 SPContext.Current.Web - >答案是否定的。
如果它是通过使用 OpenWeb() 调用打开一个新网络而返回的。答案是肯定的。
It depends on how getWeb function is returning the SPWeb.
If its just a SPContext.Current.Web - > Answer is No.
If its returned by opening a new web using OpenWeb() call. Answer is Yes.
您不能通过引用传递网络并对其进行外部处理吗?
Can you not pass the web by ref and have the outer dispose take are of it?