从 C# 中以编程方式访问 SharePoint 样式库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/424739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically Accessing SharePoint Style Library from within C#
提问by pwmusic
Firstly, I'm a newbie to C# and SharePoint, (less than a month's experience) so apologies if this is an obvious or easy question but I've been trawling the net for a couple of days now with absolutely no success.
首先,我是 C# 和 SharePoint 的新手(不到一个月的经验),如果这是一个明显或简单的问题,我深表歉意,但我已经在网上搜索了几天,但完全没有成功。
I have an xslt file that I have stored in a subdirectory of 'Style Library' from within the new website but how can I access this from within c#?
我有一个 xslt 文件,我已经将它存储在新网站中“样式库”的子目录中,但是如何从 c# 中访问它?
I've looked at SPSite and SPWeb but neither seems able to do quite what I want.
我看过 SPSite 和 SPWeb,但似乎都不能做我想要的。
Any and all help will be gratefully received.
任何和所有帮助将不胜感激。
Many thanks
非常感谢
c#newbie
c#新手
采纳答案by Ray Booysen
Here is a bit of code to retrieve the list items from a list:
这是从列表中检索列表项的一些代码:
SPList list = web.Lists["MyLibrary"];
if (list != null)
{
var results = from SPListItem listItem in list.Items
select new
{
xxx = (string)listItem["FieldName"]),
yyy = (string)listItem["AnotherField"],
zzz = (string)listItem["Field"]
};
}
To retrieve a file you could also use this method on SPWeb: GetFileAsString
要检索文件,您也可以在 SPWeb 上使用此方法:GetFileAsString
回答by Nick
回答by Jason
without linq:
没有 linq:
int itemId = getItemId();
SPWeb currentWeb = SPContext.Current.Web;
SPList list = currentWeb.Lists["MyList"];
if ( list != null )
{
SPListItem theItem = list.Items.GetItemById(itemId);
doWork(theItem);
}
The SPWeb can be retrieved in numerous ways, using the SPContext will work if the code is called from SharePoint. To get an SPWeb object from a URL you can use SPSite object i.e.
可以通过多种方式检索 SPWeb,如果从 SharePoint 调用代码,则使用 SPContext 将起作用。要从 URL 获取 SPWeb 对象,您可以使用 SPSite 对象,即
using ( SPSite site = new SPSite(urlToWeb) )
{
using (SPWeb web = site.OpenWeb())
{
doWork(web);
}
}
the 'using' statement ensures non-managed resources are reclaimed in a timely manner, by calling 'Dispose()' on the relevant objects.
“using”语句通过在相关对象上调用“Dispose()”,确保及时回收非托管资源。
HTH, jt
HTH, jt
回答by pwmusic
Many thanks for your assistance with this. I've used a little bit from each and done some additional reading and have come up with the following:
非常感谢您对此的帮助。我从每一个中使用了一点,并做了一些额外的阅读,并提出了以下几点:
private static string getXsl()
{
string xslString = null;
using (StreamReader streamReader = new StreamReader(
File.Open(HttpContext.Current.Server.MapPath(@"~_layouts\theXSL.xslt"), FileMode.Open)))
{
xslString = streamReader.ReadToEnd();
}
return xslString;
}
回答by user15916
Effective as that may be, you should reallylook into best practices as they relate to storing documents in the 12 hive versus the content database.
尽管这可能很有效,但您应该真正研究最佳实践,因为它们与将文档存储在 12 hive 和内容数据库中有关。
There are much more scalable answers, which should be considered before you choose the lemming route.
有更多可扩展的答案,在您选择旅鼠路线之前应该考虑这些答案。