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>");
}
}
}
}
}
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
SPSiteconstructor.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.