locked
SharePoint Client Object Model - ServerRelativeURL not initialized RRS feed

  • Question

  • I originally posted under RowLimit from Client Object Model but have gotten past the row limit issue.  My problem is that I am able to access lists from the client and return list items but the RootFolder.ServerRelativeURL gives this error:

    The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

    I am currently inserting to another list by using ListItemCreationInformation object but in this case, I need to load a PDF into the library so I'm trying to do an add against the list folder but since I can't get the relative path, I can't execute the add.  I can access the list, and iterate through the properties but can't get the URL (SchemaXML

     CamlQuery query = new CamlQuery();

    query.ViewXml = @"<View><Query></Query><RowLimit>10</RowLimit></View>";
    List archiveList = ctx.Web.Lists.GetByTitle("Doc Archive");
    ctx.Load(archiveList);
    ctx.ExecuteQuery();

    ListItemCollection listItems = archiveList.GetItems(query);
    ctx.Load(listItems, li =>
    li.Include(
    i => i["CARPOS_x0020_FCN"],
    i => i["Case_x0020_Number"],
    i => i["Court_x0020_Code"],
    i => i["Doc_x0020_Type"],
    i => i["CMU_x0020_Branch"]
    ).Where(i => (string)i["CARPOS_x0020_FCN"] != serviceTicket.fcnNumber));
    ctx.ExecuteQuery();

    Folder folder = archiveList.RootFolder;
    var fileInfo = new FileCreationInformation();
    fileInfo.Content = serviceTicket.ticketPDF;
    fileInfo.Overwrite = true;
    fileInfo.Url = archiveList.RootFolder.ServerRelativeUrl;
    SP.File file = folder.Files.Add(fileInfo);

     

    I did not create the lists in Sharepoint and don't know their location.  Is this something that had to be set up?  I don't know what my next steps would be at this point.

     

    Thanks,

    Jim

     

    Monday, February 14, 2011 6:58 PM

Answers

  • Hi Jim,

    Please insert this code

     

    ctx.Load(folder);
    ctx.ExecuteQuery();
    

     

    after your line

     

    Folder folder = archiveList.RootFolder;
    
    and it should work

     

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    • Marked as answer by Jim Stallings Monday, February 14, 2011 10:33 PM
    Monday, February 14, 2011 10:27 PM

All replies

  • Sorry, meant to say SchemaXML has the same issue among other properties.
    Monday, February 14, 2011 6:59 PM
  • Hi Jim,

    Please insert this code

     

    ctx.Load(folder);
    ctx.ExecuteQuery();
    

     

    after your line

     

    Folder folder = archiveList.RootFolder;
    
    and it should work

     

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    • Marked as answer by Jim Stallings Monday, February 14, 2011 10:33 PM
    Monday, February 14, 2011 10:27 PM
  • Yep, that was it.  Thank you Dmitry
    Monday, February 14, 2011 10:33 PM
  • Hi Dmitry,

    I'm using sharepoint online 2013 ,

     List announcementsList = clientContext.Web.Lists.GetByTitle("ImageLib");

                     
                    CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
                    ListItemCollection items = announcementsList.GetItems(query);

                    
                    clientContext.Load(items);
                    clientContext.ExecuteQuery();
                    Folder folder = announcementsList.RootFolder;

                    string test=items.GetById(3).File.ServerRelativeUrl.ToString();

     foreach (var listItem in items)
                    {
                        string[] urlfilepath = listItem.FieldValues.ElementAt(10).ToString().Split(',');
                        string[] urlpath = urlfilepath[1].ToString().Split(']');
                        string finalpath = urlpath[0].ToString();

                        using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, listItem.File.ServerRelativeUrl))
                        {
                            using (System.IO.Stream destFile = System.IO.File.OpenWrite(@"D:\dr.ait projects\monika"))
                            {
                                byte[] buffer = new byte[8 * 1024];
                                int len;
                                while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    destFile.Write(buffer, 0, len);
                                }
                            }
                        }

    }

    facing error: The property or field 'ServerRelativeUrl' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

    Plz Help me....

    Ashwini

    Tuesday, May 27, 2014 11:01 PM
  • Ashwini,

    Just before using the 'ServerRelativeUrl' property you must explicitly retrieve it.  You can do this like so:

    Before your line of code where you're referencing the property you're already loading the 'items' in the current context:

    clientContext.Load(items);
    clientContext.ExecuteQuery();
    Folder folder = announcementsList.RootFolder;

    string test=items.GetById(3).File.ServerRelativeUrl.ToString();

    You just need to modify the load to explicitly retrieve the property and send the request to the server to execute:

    clientContext.Load(items, i => i.GetById(3).File.ServerRelativeUrl);
    clientContext.ExecuteQuery();

    Hope this helps anyone else facing the same problem.


    • Edited by Izzo Wednesday, January 21, 2015 3:42 PM
    Wednesday, January 21, 2015 3:41 PM
  • I am facing a similar issue, but loading the property as described does not seem to work:

                        

    Dim list = ctx.Web.Lists.GetByTitle(listTitle)

            Dim listItem = list.GetItemById(listItemId)
            Dim cq = CamlQuery.CreateAllItemsQuery()
            Dim lic = list.GetItems(cq)
    'trying to load ServerRelativeUrl along with default properties -> does not work
            ctx.Load(lic, Function(i) i.IncludeWithDefaultProperties(Function(i2) i2.File.ServerRelativeUrl))

            ctx.ExecuteQuery()
            


            For Each l As ListItem In lic
                l = l
                Dim ff = l.File
       'Trying to load ServerRelativeUrl explicitly for each file does not work either
                ctx.Load(ff, Function(i2) i2.ServerRelativeUrl)
                ctx.ExecuteQuery()
                Dim fileref As String
    'Error thrown here:
                fileref = ff.ServerRelativeUrl


                Dim fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileref)
                Dim fileName = Path.Combine(filePath, DirectCast(listItem.File.Name, String))
                Using fileStream = System.IO.File.Create(fileName)
                    fileInfo.Stream.CopyTo(fileStream)
                End Using
            Next


    Note: I am using VB.NET, so the labda expression looks a bit different.

    It does work for e.g. listitem.displayname.


    Wednesday, May 13, 2015 8:32 PM