基本 SharePoint Web 部件 C#

发布于 2024-10-03 05:13:19 字数 838 浏览 4 评论 0原文

我以前从未使用过 C# 工作(尽管使用过不少 PHP),但我的任务是制作 SharePoint Web 部件。我需要它从 SharePoint 中提取列表数据。我真的不知道应该从哪里开始,所以我从创建一个“检查列表是否存在”功能开始。这只是检查字符串“listName”并将“此列表存在”打印到页面。

任何人都可以帮助我编写代码并至少让我走上正确的轨道吗?谢谢。

namespace SlideShowWebPart
  {
    public class SlideShowGuide : WebPart
      {
        protected override void Render(System.Web.UI.HtmlTextWriter output)
          {
            using (SPWeb web = SPContext.Current.Site.OpenWeb("http://www.school.edu"))
              {
                string listName = "List123ABC";
                var lists = web.Lists;
                foreach (SPList list in lists)
                  {
                    if (list.Title.Equals(listName))
                       output.Write("<b>This list exists</b>");
                  }
              }
          }
      }
  }

I've never done work in C# before (although quite a bit in PHP), but have been tasked to make a SharePoint webpart. I need it to pull list data from SharePoint. I really am clueless as to where I should begin, so I'm starting by making a 'check to see if list exists' function. This just checks for the string 'listName' and prints 'this list exists' to the page.

Can anyone help me with my code and at least set me on the rigth track? Thanks.

namespace SlideShowWebPart
  {
    public class SlideShowGuide : WebPart
      {
        protected override void Render(System.Web.UI.HtmlTextWriter output)
          {
            using (SPWeb web = SPContext.Current.Site.OpenWeb("http://www.school.edu"))
              {
                string listName = "List123ABC";
                var lists = web.Lists;
                foreach (SPList list in lists)
                  {
                    if (list.Title.Equals(listName))
                       output.Write("<b>This list exists</b>");
                  }
              }
          }
      }
  }

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

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

发布评论

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

评论(2

寄与心 2024-10-10 05:13:19

以下内容可能不准确,因为在不查看堆栈跟踪的情况下很难识别问题。

SPSite.OpenWeb() 采用相对 URL。尝试删除主机名并仅包含路径。 (在您的示例中,没有列出路径)。如果您尝试打开当前的网页,那么您应该可以调用无参数重载。

SPSite.OpenWeb() 文档

using (SPWeb web = SPContext.Current.Site.OpenWeb())
{
    // Your Code Here
}

如果您需要要使用 URL 访问您的网站,您可以将绝对 URL 传递到 SPSite 构造函数中。

SPSite(string) 构造函数文档

using (SPSite site = new SPSite("http://www.school.edu"))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Your Code Here
    }
}

,您可以像这样获取对当前 Web 的引用:

SPWeb web = SPContext.Current.Web;

请小心不要处置从 SPContext.Current 获取的对象,因为这将导致 SharePoint 出现问题。

The following may not be accurate, as it can be difficult to identify the issue without seeing a stack trace.

SPSite.OpenWeb() takes a relative URL. Try removing the host name and including only the path. (In your example, there is no path listed). If you are trying to open the current web, then you should be fine calling the no-parameter overload.

SPSite.OpenWeb() Documentation

using (SPWeb web = SPContext.Current.Site.OpenWeb())
{
    // Your Code Here
}

If you need to use the URL to access your site, you can pass an absolute url into the SPSite constructor.

SPSite(string) Constructor Documentation

using (SPSite site = new SPSite("http://www.school.edu"))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Your Code Here
    }
}

Alternatively, you can get a reference to the current web like this:

SPWeb web = SPContext.Current.Web;

Be careful not to dispose of objects that you obtain from SPContext.Current, as this will cause issues with SharePoint.

墨小沫ゞ 2024-10-10 05:13:19

您可以使用 web.lists["listName"] 检查列表是否存在。获得列表后,您可以通过相同的概念引用这些项目。

SPList list = web.lists["listName"];
list.items["ColumnName"]; // returns the value as a string representation

如果您只想访问列表数据,请为自己获取一份 Sharepoint Designer 的副本,导航至 www.endusersharepoint.com 并查看数据视图 Web 部件上的文章系列。

you can use web.lists["listName"] to check for existence of a list. Once you have the list you can reference the items via the same concept.

SPList list = web.lists["listName"];
list.items["ColumnName"]; // returns the value as a string representation

If you are looking to just get access to list data, grab yourself a copy of sharepoint designer, navigate over to www.endusersharepoint.com and check out the article series on the data view web part.

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