2

Error related to System.IO.FileNotFoundException.

Trying to get Survey 1, Survey 2 from Custom list Survey with 2 Columns:

  1. Title
  2. Url - Contains URL of Survey 1 or Survey 2

Custom Survey list Contain Following Survey

  1. Survey 1 - Survey [out of box]
  2. Survey 2 - Survey [out of box]

Reference Code:

SPSecurity.RunWithElevatedPrivileges(delegate() { 
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Survey list"];

        foreach (SPListItem item in itemCollections)
        {
            string Url = item["URL"].ToString();
            string[] url=Url.Split(',');
            var listUrl = url[0];  //http://pc6/Surveys/Lists/Survey 1
            string relativeUrl = new Uri(listUrl).AbsolutePath; // /Surveys/Lists/Survey%201
            var ServerRelativeurl="/"+relativeUrl.Split('/')[1];
            var surveyListUrl=relativeUrl.Split('/')[2];
            var surveyUrl = relativeUrl.Split('/')[3];

            SPWeb surveyWeb = site.OpenWeb(ServerRelativeurl);
            var relativeUrlForList = "/" + surveyListUrl + "/" + surveyUrl;
            // Error
            SPList List = surveyWeb.GetList(relativeUrlForList);
        }
    }
});
4
  • have you printed out all the string manipulation you're doing? Are you sure the list URL is correct? Try printing it out and accessing it with powershell to make it easier to test. Also, your surveyWeb isn't being disposed.
    – Choggo
    Jan 15 '15 at 13:54
  • Yes m,sure list URL is correct. Jan 15 '15 at 14:06
  • What is "itemCollections"? Is it your "list.GetItems()"? Put a using around your surveyWeb-object for disposing the object. Besides that your code seems to be working, if the URL field is set properly (although it could still be cleaned up a bit and your variable naming like "Url" and "url" should not only differ by upper/lower case, chose something self-explanatory that's not so irritating). Jan 15 '15 at 16:09
  • SPListItemCollection itemCollections = list.GetItems(query); Jan 16 '15 at 4:38
1

The way you split and join URLs is a little messy at best, so I'm not sure I understand it correctly.

It seems that you try to open a list using /Lists/Survey 1 URL. This is web-relative URL. According to SPWeb.GetList() you should use server-relative URL (like /Surveys/Lists/Survey 1).

0

I suggest you to use this code:

string strUrl = "http://url";
SPSite siteCollection = newSPSite(strUrl);
SPWeb web  = siteCollection.OpenWeb();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.