none
handle urls which are not found. RRS feed

  • Question

  • Hi all,

    i have a master list with all site urls and iam retrieving all sites using csom and adding users to the groups for those sites.

    In that some urls not exist means when open 404 not found error.How to handle this urls to avoid.please suggest.

    Thanks

    praveen

    Tuesday, February 19, 2019 4:04 PM

Answers

  • Hi,

    The full code as below for your reference.

    namespace ConsoleApplication1
    {
        public static class Program
        {
            static bool WebExistsFullUrl(this ClientRuntimeContext context, string webFullUrl)
            {
                bool exists = false;
                try
                {
                    using (ClientContext testContext = context.Clone(webFullUrl))
                    {
                        testContext.Load(testContext.Web, w => w.Title, w => w.Url);
                        testContext.ExecuteQuery();
                        exists = (string.Compare(testContext.Web.Url, webFullUrl, true) == 0);
                    }
                }
                catch (Exception ex)
                {                
                }
                return exists;
            }
    
            static void  Main(string[] args)
            {
    
                ClientContext ctx = new ClientContext("http://sp2010/sites/team");
                ctx.Credentials = new NetworkCredential("username", "password", "domain");
                var list = ctx.Web.Lists.GetByTitle("SiteUrlList");
                CamlQuery query = new CamlQuery();
                var items = list.GetItems(query);
                ctx.Load(items);
                ctx.ExecuteQuery();
                
                foreach(var item in items)
                {
                    if (!WebExistsFullUrl(ctx, item["Title"].ToString()))
                    { 
                        //delete item if the site not exists
                        item.DeleteObject();
                        ctx.ExecuteQuery();
                    }                                           
                }
                //Console.ReadKey();
            }     
        }    
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Praveenrangu Thursday, February 21, 2019 8:05 AM
    Thursday, February 21, 2019 1:23 AM
    Moderator

All replies

  • Hi

    Here is a reference for you.

    https://blog.mastykarz.nl/sharepoint-2010-page-not-found-404/


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Wednesday, February 20, 2019 2:43 AM
  • Hi Praveen,

    We can use a job to maintain the list items, if the site url not exists, we can delete this item using CSOM C#.

    Example, I use Title field to store my all site urls of a custom list "SiteUrlList", and use the code below to maintain the list.

    static bool WebExistsFullUrl(this ClientRuntimeContext context, string webFullUrl)
    {
    	bool exists = false;
    	try
    	{
    		using (ClientContext testContext = context.Clone(webFullUrl))
    		{
    			testContext.Load(testContext.Web, w => w.Title, w => w.Url);
    			testContext.ExecuteQuery();
    			exists = (string.Compare(testContext.Web.Url, webFullUrl, true) == 0);
    		}
    	}
    	catch (Exception ex)
    	{                
    	}
    	return exists;
    }
    
    static void  Main(string[] args)
    {
    
    	ClientContext ctx = new ClientContext("http://sp2010/sites/team");
    	ctx.Credentials = new NetworkCredential("username", "password", "domain");
    	var list = ctx.Web.Lists.GetByTitle("SiteUrlList");
    	CamlQuery query = new CamlQuery();
    	var items = list.GetItems(query);
    	ctx.Load(items);
    	ctx.ExecuteQuery();
    	
    	foreach(var item in items)
    	{
    		if (!WebExistsFullUrl(ctx, item["Title"].ToString()))
    		{ 
    			//delete item if the site not exists
    			item.DeleteObject();
    			ctx.ExecuteQuery();
    		}                                           
    	}
    	//Console.ReadKey();
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Wednesday, February 20, 2019 3:04 AM
    Moderator
  • thanks for the reply iam using sp 2010 csom but iam not getting  executequeryretry property is not coming.

    • Edited by Praveenrangu Wednesday, February 20, 2019 7:57 AM
    Wednesday, February 20, 2019 7:56 AM
  • Hi,

    Please replace ExecuteQueryRetry with ExecuteQuery, I have modified the code in reply above.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, February 20, 2019 8:56 AM
    Moderator
  • if i add this method iam getting error like extention method must be defined in a non generic static class.
    Wednesday, February 20, 2019 9:48 AM
  • Hi,

    The full code as below for your reference.

    namespace ConsoleApplication1
    {
        public static class Program
        {
            static bool WebExistsFullUrl(this ClientRuntimeContext context, string webFullUrl)
            {
                bool exists = false;
                try
                {
                    using (ClientContext testContext = context.Clone(webFullUrl))
                    {
                        testContext.Load(testContext.Web, w => w.Title, w => w.Url);
                        testContext.ExecuteQuery();
                        exists = (string.Compare(testContext.Web.Url, webFullUrl, true) == 0);
                    }
                }
                catch (Exception ex)
                {                
                }
                return exists;
            }
    
            static void  Main(string[] args)
            {
    
                ClientContext ctx = new ClientContext("http://sp2010/sites/team");
                ctx.Credentials = new NetworkCredential("username", "password", "domain");
                var list = ctx.Web.Lists.GetByTitle("SiteUrlList");
                CamlQuery query = new CamlQuery();
                var items = list.GetItems(query);
                ctx.Load(items);
                ctx.ExecuteQuery();
                
                foreach(var item in items)
                {
                    if (!WebExistsFullUrl(ctx, item["Title"].ToString()))
                    { 
                        //delete item if the site not exists
                        item.DeleteObject();
                        ctx.ExecuteQuery();
                    }                                           
                }
                //Console.ReadKey();
            }     
        }    
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Praveenrangu Thursday, February 21, 2019 8:05 AM
    Thursday, February 21, 2019 1:23 AM
    Moderator