从 C# 中以编程方式访问 SharePoint 样式库

发布于 2024-07-11 15:05:09 字数 246 浏览 12 评论 0 原文

首先,我是 C# 和 SharePoint 的新手(不到一个月的经验),所以如果这是一个明显或简单的问题,我深表歉意,但我已经在网上搜索了几天,但完全没有成功。

我有一个 xslt 文件,已存储在新网站中“样式库”的子目录中,但如何从 c# 中访问该文件?

我查看过 SPSite 和 SPWeb,但似乎都无法满足我的要求。

任何和所有的帮助将不胜感激。

非常感谢

c#newbie

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.

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#?

I've looked at SPSite and SPWeb but neither seems able to do quite what I want.

Any and all help will be gratefully received.

Many thanks

c#newbie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

许你一世情深 2024-07-18 15:05:09

以下是从列表中检索列表项的一些代码:

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"]
                              };
            }

要检索文件,您还可以在 SPWeb 上使用此方法: GetFileAsString

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

孤城病女 2024-07-18 15:05:09

Patrick,

我希望您喜欢 C# 和 SharePoint!

查看文章 此处

仔细阅读该内容,它应该会为您提供所需的所有帮助。

缺口。

Patrick,

I hope you enjoy both C# and SharePoint!

Check out the article here.

Read that through, and it should give you all the assistance you need.

Nick.

你的背包 2024-07-18 15:05:09

不使用 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);
}

可以通过多种方式检索 SPWeb,如果从 SharePoint 调用代码,则可以使用 SPContext。 要从 URL 获取 SPWeb 对象,您可以使用 SPSite 对象,即

using ( SPSite site = new SPSite(urlToWeb) )
{
   using (SPWeb web = site.OpenWeb())
   {
     doWork(web);
   }
}

“using”语句通过在相关对象上调用“Dispose()”来确保及时回收非托管资源。

哈特哈,
杰特

without 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.

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.

HTH,
jt

无所谓啦 2024-07-18 15:05:09

尽管这可能很有效,但您应该真正研究最佳实践,因为它们与将文档存储在 12 个配置单元与内容数据库中相关。

在选择旅鼠路线之前,应该考虑更多可扩展的答案。

Effective as that may be, you should really look into best practices as they relate to storing documents in the 12 hive versus the content database.

There are much more scalable answers, which should be considered before you choose the lemming route.

终难愈 2024-07-18 15:05:09

非常感谢您对此的协助。 我使用了其中的一些内容并进行了一些额外的阅读,并得出了以下结论:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文