到目前为止,在我的研究中,我已经看到在GET请求操作上设置AllowUnsafeUpdates是不明智的,以避免跨站点脚本.但是,如果要求允许这样做,处理这种情况以减轻任何暴露的正确方法是什么?
如果您绝对需要在GET请求上允许Web或站点更新,这是我对可靠模式的最佳猜测.
最佳实践?
protected override void OnLoad(System.EventArgs e)
{
if(Request.HttpMethod == "POST")
{
SPUtility.ValidateFormDigest();
// will automatically set AllowSafeUpdates to true
}
// If not a POST then AllowUnsafeUpdates should be used only
// at the point of update and reset immediately after finished
// NOTE: Is this true? How is cross-site scripting used on GET
// and what mitigates the vulnerability?
}
// Point of item update
using(SPSite site = new SPSite(SPContext.Current.Site.Url, SPContext.Current.Site.SystemAccount.UserToken))
{
using (SPWeb web = site.RootWeb) …Run Code Online (Sandbox Code Playgroud) 所以我在一个网络 scroped功能(properties.Feature.Parent= SPWeb).
我怎么得到SPWebApplication这个SPWeb?
我试过了:
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
Run Code Online (Sandbox Code Playgroud)
但当然我得到一个例外,因为我无法将SPWeb对象转换为SPWebApplication.
我希望有人可以帮助我.我需要从SPContext获取当前站点的根网站.通过以下方式轻松完成
SPContext.Current.Site.RootWeb
Run Code Online (Sandbox Code Playgroud)
我对SPConite.Current中SPSite对象的想法很满意.站点 .RootWeb不应该被处理掉,但是我从SPSite得到的SPWeb对象呢.当SPSite得到处置时,rootweb SPWeb会被处理掉吗?或者我需要自己处理它?
我正在开发一个项目,我将docx文件添加到Visual Studio中的Layout文件夹中,然后使用这些ducment文件创建内容类型.
问题是我无法以编程方式获取文档文件.使用Web浏览器我可以获取文件但不使用web.GetFolder();. 激活功能时,代码在功能接收器中运行.
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;
SPFolder docTempFolder = web.GetFolder("_LAYOUTS/Projekt/DocumentTemplates");
Run Code Online (Sandbox Code Playgroud)
这段代码给了我一个零文件的集合.
我究竟做错了什么?
谢谢你的帮助.
我正在使用创建一个SharePoint站点
SPWeb spWeb = spSite.AllWebs.Add(...);
Run Code Online (Sandbox Code Playgroud)
我需要运行什么代码来设置spWeb以关闭"在导航中显示页面"选项?
回答:
publishingWeb.IncludePagesInNavigation = false;
Run Code Online (Sandbox Code Playgroud) 我很高兴继承了一个非常复杂的SharePoint项目.
显然,原始开发人员是可重用代码的忠实粉丝(30%的代码在不使用任何库的情况下在20个项目中重复使用 - 猜怎么样?).
我经常会发现他的代码调用一些Common.OpenWeb方法来检索SPWeb操作SharePoint内容的对象.这个函数的大部分内容看起来完全相同:
public SPWeb OpenWeb()
{
String strSiteUrl = ConfigurationManager.AppSettings["SiteUrl"].ToString();
SPSite site = null;
SPWeb web = null;
try
{
using (site = new SPSite(strSiteUrl))
{
using (web = site.OpenWeb())
{
return web;
}
}
}
catch (Exception ex)
{
LogEvent("Error occured in OpenWeb : " + ex.Message, EventLogEntryType.Error);
}
return web;
}
Run Code Online (Sandbox Code Playgroud)
而现在我真的很担心.
为什么这在生产中有效?这个方法总是返回一个被处理的对象,对吧?
它到底有多不稳定?
更新:
此方法以下列方式使用:
oWeb = objCommon.OpenWeb();
SPList list = oWeb.Lists["List name"];
SPListItem itemToAdd = list.Items.Add();
itemToAdd["Some field"] = …Run Code Online (Sandbox Code Playgroud) 有谁可以解释SP.Web和SP.Site实体之间的区别?据我所知,SP.Site包含不同的SP.Webs,代表SiteCollections.这是对的吗?您能否分享一下链接以更好地理解这种方法?
我只是想知道如何在代码中找出内存泄漏.我正在处理由其他人编写的代码,并被告知它有内存泄漏.我正在通过代码来查看它是否有内存泄漏.
以下代码是否有内存泄漏.我需要在这里关闭SPWEB对象吗?
private bool isSubSite()
{
SPWeb currWeb = SPContext.Current.Web;
SPWeb rootWeb = currWeb.Site.RootWeb;
if (currWeb.ID != rootWeb.ID)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud) 在我的PowerShell脚本中,我想获得用于创建SPWeb的SPWebTemplate.在SPWeb实例中,可以访问属性WebTemplate和WebTemplateId,但它们都不能唯一地标识SPWebTemplate.
例如,如果已使用SPWebTemplate"STS#0"创建SPWeb,则WebTemplate将包含"STS",WebTemplateId将为"1".
现在
Get-SPWebTemplate | where { $_.ID -eq 1 }
Run Code Online (Sandbox Code Playgroud)
将导致每种安装语言的3个结果(STS#0,STS#1,STS#2).如何检索正确的SPWebTemplate-Name(STS#1)?
在此先感谢,
乔纳斯
可能重复:
什么是C#使用块,为什么要使用它?
如果我运行以下代码:
using (SPWeb web = SPContext.Current.Site.OpenWeb("/myweb")){
//error happens here
}
Run Code Online (Sandbox Code Playgroud)
如果在结束括号之前发生错误web,是否会正确处理对象?
我们被告知使用对象的using语句OpenWeb是最好的,但
我们在日志中看到很多关于SPRequests和SPWeb的错误.
spweb ×11
sharepoint ×8
c# ×4
spsite ×3
idisposable ×2
using ×2
dispose ×1
memory ×1
memory-leaks ×1
moss ×1
navigation ×1
powershell ×1