0

I am using the .net client object model for SharePoint 2010. I am able to create a new wiki page in my wikipagelibrary using the following method:

    Dim wikiLibrary = context.Web.Lists.GetByTitle(wikiPageLibraryTitle)
    context.Load(wikiLibrary.RootFolder)
    context.ExecuteQuery()

    Dim relativeUrl As String = "/sites/kdb/" & txt.Text & "/" & pageName
    Dim fileAlreadyExcists As Boolean = CheckIfWikiPageExcists(context, relativeUrl)

    If Not fileAlreadyExcists Then
        Dim pageUrl = String.Format("{0}/{1}", wikiLibrary.RootFolder.ServerRelativeUrl, pageName)
        Dim wikiFile = wikiLibrary.RootFolder.Files.AddTemplateFile(pageUrl, TemplateFileType.WikiPage)
        context.Load(wikiFile)
        context.ExecuteQuery()

        Dim wikiPage = wikiFile.ListItemAllFields
        wikiPage("WikiField") = pageContent
        wikiPage.Update()
        context.ExecuteQuery()
    Else
        MessageBox.Show("Deze Pagina bestaat al!")
    End If

One of the parameters of this method is "wikiPageLibraryTitle". How can I get that title if i only know the URL of the library?

1 Answer 1

0

There is no built-in method for retrieving List by its Url, in C# CSOM the following method could be used for that purpose:

public static List LoadListByUrl(ClientContext context,string listUrl)
{
   var lists = context.LoadQuery(context.Web.Lists.Where(l => l.RootFolder.ServerRelativeUrl == listUrl));
   context.ExecuteQuery();
   return lists.FirstOrDefault();
}

Unfortunately the specified method could not be directly converted into VB.NET CSOM due to the limitation with LINQ expressions in VB.NET, for example, the following function:

Public Function GetListByUrl(ByVal context As ClientContext, ByVal listUrl As String) As List
    Dim query = context.LoadQuery( _
                context.Web.Lists.Where(Function(l) l.RootFolder.ServerRelativeUrl = listUrl))
    context.ExecuteQuery()
    Return query.FirstOrDefault()
End Function

will return Microsoft.SharePoint.Client.ClientRequestException exception.


Solution

But, you could use the following function to load List by its Url:

Public Function LoadListByUrl(ByVal context As ClientContext, ByVal listUrl As String) As List
    context.Load(context.Web.Lists, Function(lcol) lcol.Include(Function(l) l.RootFolder))
    context.ExecuteQuery()
    Return context.Web.Lists.SingleOrDefault(Function(l) l.RootFolder.ServerRelativeUrl = listUrl)
End Function

Usage

Load Pages list located on news web site:

Dim listUrl = "/news/Pages"
Dim list = LoadListByUrl(ctx, listUrl)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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