1

I am trying to get List item values from SharePoint Online List using CSOM console application.

When executing it I am getting error:

invalid URI: hostname could not be parsed error.

I am not sure what could be the issue. URL is valid only.

enter image description here

May I know what could be the issue?

  • Can you please check if the URL is correct or not? Can you add the URL format your are using ? – Ganesh Sanap Nov 13 at 8:56
  • url format is https://{Rootsite}/sites/sitename – Sriram Tamilarasan Nov 13 at 9:10
1

As per my understanding there must be something wrong with the URL you written, maybe some extra spaces, : or / characters in your URL.

However you can get the ClientContext directly using below code (No need to use Uri object):

ClientContext context = new ClientContext("http://SiteUrl");

Use below code to get the SharePoint Online site ClientContext and load all lists in your site (just for an example):

static void Main(string[] args)
{
    Console.WriteLine("SharePoint Online site URL:");
    string webSPOUrl = Console.ReadLine();
    Console.WriteLine("User Name:");
    string userName = Console.ReadLine();
    Console.WriteLine("Password:");
    SecureString password = FetchPasswordFromConsole();
    try
    {
        using (var context = new ClientContext(webSPOUrl))
        {
            context.Credentials = new SharePointOnlineCredentials(userName, password);
            Web web = context.Web;
            context.Load(web.Lists,
                lists => lists.Include(list => list.Title,
                    list => list.Id));
            context.ExecuteQuery();
            Console.ForegroundColor = ConsoleColor.White;
            foreach (List list in web.Lists)
            {
                Console.WriteLine("List title is: " + list.Title);
            }
            Console.WriteLine("");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error is: " + ex.Message);
    }
}

private static SecureString FetchPasswordFromConsole()
{
    string password = "";
    ConsoleKeyInfo info = Console.ReadKey(true);
    while (info.Key != ConsoleKey.Enter)
    {
        if (info.Key != ConsoleKey.Backspace)
        {
            Console.Write("*");
            password += info.KeyChar;
        }
        else if (info.Key == ConsoleKey.Backspace)
        {
            if (!string.IsNullOrEmpty(password))
            {
                password = password.Substring(0, password.Length - 1);
                int pos = Console.CursorLeft;
                Console.SetCursorPosition(pos - 1, Console.CursorTop);
                Console.Write(" ");
                Console.SetCursorPosition(pos - 1, Console.CursorTop);
            }
        }
        info = Console.ReadKey(true);
    }
    Console.WriteLine();
    SecureString securePassword = new SecureString();
    //Convert string to secure string  
    foreach (char c in password)
    {
        securePassword.AppendChar(c);
    }
    securePassword.MakeReadOnly();
    return securePassword;
}

Note: Your SharePoint Online site URL will be in format: https://domain.sharepoint.com/sites/TestSite

Official documentation:

Complete basic operations using SharePoint client library code.

  • Initially i have tried as mentioned you, it failed and same error. then only passed url field. Again same error. – Sriram Tamilarasan Nov 13 at 9:09
  • Ok, Check the updated answer. Try the given code and let me know if it works or if you face any issues. – Ganesh Sanap Nov 13 at 9:17
  • 1
    Thanks Ganesh, your code is working fine. Not sure what is wrong with my code. :) – Sriram Tamilarasan Nov 13 at 9:51
  • Welcome, glad it worked for you. – Ganesh Sanap Nov 13 at 9:56

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.