基本 SharePoint Web 部件 C#
我以前从未使用过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容可能不准确,因为在不查看堆栈跟踪的情况下很难识别问题。
SPSite.OpenWeb()
采用相对 URL。尝试删除主机名并仅包含路径。 (在您的示例中,没有列出路径)。如果您尝试打开当前的网页,那么您应该可以调用无参数重载。SPSite.OpenWeb() 文档
如果您需要要使用 URL 访问您的网站,您可以将绝对 URL 传递到
SPSite
构造函数中。SPSite(string) 构造函数文档
,您可以像这样获取对当前 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
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
Alternatively, you can get a reference to the current web like this:
Be careful not to dispose of objects that you obtain from SPContext.Current, as this will cause issues with SharePoint.
您可以使用 web.lists["listName"] 检查列表是否存在。获得列表后,您可以通过相同的概念引用这些项目。
如果您只想访问列表数据,请为自己获取一份 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.
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.