{"id":876,"date":"2019-01-25T07:13:23","date_gmt":"2019-01-25T07:13:23","guid":{"rendered":"http:\/\/blog.softreeconsulting.com\/?p=876"},"modified":"2019-01-25T07:13:23","modified_gmt":"2019-01-25T07:13:23","slug":"how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/","title":{"rendered":"How To Get All Orphan Users Present In SharePoint On-Premise Site"},"content":{"rendered":"\n\nAn Orphaned User is a user account that is available in SharePoint site but that user can&#8217;t access SharePoint any longer. This can be in a case if the user account is deleted or disabled from the Active Directory.\n\n\n\n\n\nIn this blog, first, we are retrieving all the users from the user information list of the site collection, then we are checking whether those users are valid or invalid in our Active Directory.\n\n\n\n\n\nThe below image shows all my users present in the user information list of the site collection. You can get the user information list by\u00a0 navigating to &#8220;Siteurl + \/_catalogs\/users\/detail.aspx\n\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" class=\"wp-image-877\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/img1-1-1024x375.png\" alt=\"\" \/><\/figure>\n\n\n\n\n<strong>Add the below code:-<\/strong>\n\n\n\n<p>[code lang=&#8221;c&#8221;]<br \/>\nusing System;<br \/>\nusing System.Net;<br \/>\nusing Microsoft.SharePoint.Client;<br \/>\nnamespace OrphanUser<br \/>\n{<br \/>\nclass Program<br \/>\n{<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\nClientContext ctx = new ClientContext(&#8220;http:\/\/portal\/sites\/site1&#8221;);<\/p>\n<p>NetworkCredential cred = new NetworkCredential(&#8220;userName&#8221;,&#8221;passWord&#8221;);<br \/>\nctx.Credentials = cred;<\/p>\n<p>ctx.ExecuteQuery();<br \/>\nWeb web = ctx.Web;<br \/>\nListItemCollection itemColl = null;<br \/>\nUser user = null;<br \/>\nbool isGroup = false;<br \/>\nstring userName = string.Empty;<br \/>\nstring status = string.Empty;<\/p>\n<p>itemColl = web.SiteUserInfoList.GetItems(new CamlQuery());<\/p>\n<p>ctx.Load(itemColl,<br \/>\nitems =&gt; items.Include(<br \/>\nitem =&gt; item.FieldValuesAsText,<br \/>\nitem =&gt; item.Id,<br \/>\nitem =&gt; item.DisplayName));<br \/>\nctx.ExecuteQuery();<br \/>\nforeach (ListItem itm in itemColl)<br \/>\n{<br \/>\nuser = web.EnsureUser(itm.DisplayName);<br \/>\ntry<br \/>\n{<br \/>\nctx.Load(user,<br \/>\nu =&gt; u.LoginName);<br \/>\nctx.ExecuteQuery();<br \/>\nisGroup = false;<br \/>\n}<br \/>\ncatch { isGroup = true; }<br \/>\nif (!isGroup)<br \/>\n{<br \/>\nuserName = itm.DisplayName;<br \/>\nif (userName.ToLower() == &#8220;NT AUTHORITY\\authenticated users&#8221;.ToLower() ||<br \/>\nuserName.ToLower() == &#8220;Helpdesk Administrator&#8221;.ToLower() ||<br \/>\nuserName.ToLower() == &#8220;Everyone except external users&#8221;.ToLower() ||<br \/>\nuserName.ToLower() == &#8220;SharePoint\\\\SYSTEM&#8221;.ToLower() ||<br \/>\nuserName.ToLower() == &#8220;Everyone&#8221;.ToLower() ||<br \/>\nuserName.ToLower().StartsWith(&#8220;nt authority\\\\&#8221;) ||<br \/>\nuserName.ToLower() == &#8220;SharePoint App&#8221;.ToLower() ||<br \/>\nuserName.ToLower() == &#8220;System Account&#8221;.ToLower()||<br \/>\nuserName.ToLower().Contains(&#8220;_spo&#8221;))<br \/>\n{<br \/>\ncontinue;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nGetOrphanedUsers(ctx, web, itm.DisplayName);<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>}<br \/>\npublic static void GetOrphanedUsers(ClientContext ctx, Web web, string userValue)<br \/>\n{<br \/>\ntry<br \/>\n{<\/p>\n<p>Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters query =<br \/>\nnew Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters();<br \/>\nquery.AllowEmailAddresses = false;<br \/>\nquery.AllowMultipleEntities = false;<br \/>\nquery.ForceClaims = false;<br \/>\nquery.MaximumEntitySuggestions = 50;<br \/>\nquery.PrincipalType = Microsoft.SharePoint.Client.Utilities.PrincipalType.All;<br \/>\nquery.PrincipalSource = Microsoft.SharePoint.Client.Utilities.PrincipalSource.All;<br \/>\nquery.QueryString = userValue;<br \/>\nquery.AllUrlZones = false;<br \/>\nquery.SharePointGroupID = 0;<br \/>\nquery.WebApplicationID = new Guid(&#8220;00000000-0000-0000-0000-000000000000&#8221;);<br \/>\nClientResult&lt;String&gt; resultInfo =<br \/>\nMicrosoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerWebServiceInterface.ClientPeoplePickerSearchUser(ctx, query);<\/p>\n<p>try<br \/>\n{<br \/>\nctx.ExecuteQuery();<br \/>\n}<br \/>\ncatch { }<\/p>\n<p>if (resultInfo == null || resultInfo.Value == null || resultInfo.Value == &#8220;[]&#8221;)<br \/>\n{<br \/>\nConsole.WriteLine(userValue + &#8221; is an Orphan user&#8221;);<br \/>\n}<br \/>\n}<br \/>\ncatch { }<br \/>\n}<\/p>\n<p>}<br \/>\n}<br \/>\n[\/code]<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" class=\"wp-image-879\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/img2-2.png\" alt=\"\" \/><figcaption><\/figcaption><\/figure>\n<\/div>\n\n\n\n\n<strong>Result:<\/strong>\n\n\n\n\n\nIt shows only the user accounts that are deleted or disabled from the Active Directory.\n\n\n\n\n\n<strong>This solution is brought to you by our SharePoint professionals.<\/strong>\n\n\n\n\n\n<a href=\"http:\/\/www.softreeconsulting.com\/\"><strong>Softree Consulting<\/strong><\/a>\u00a0employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.\n\n\n\n\n\nBe it SPFx or SharePoint add-in developments,\u00a0<a href=\"http:\/\/www.softreeconsulting.com\/sharepoint-2019\/\"><strong>SharePoint 2019 developments<\/strong><\/a>, web part developments, migrating from SharePoint 2010\/2013 to SharePoint 2013\/2016\/Office 365, Office 365,\u00a0SharePoint hosted apps development\u00a0or something else in SharePoint, we strive to deliver the best\n\n","protected":false},"excerpt":{"rendered":"<p>An Orphaned User is a user account that is available in SharePoint site but that user can&#8217;t access SharePoint any longer. This can be in a case if the user account is deleted or disabled from the Active Directory. In this blog, first, we are retrieving all the users from the user information list of the site collection, then we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[209,210,211],"class_list":["post-876","post","type-post","status-publish","format-standard","hentry","category-sharepoint","tag-get-all-orphan-users-from-sharepoint-onpremise-site","tag-how-to-get-orphan-users-of-a-site-collection","tag-retrieve-all-orphan-users-of-a-site-using-csom"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Get All Orphan Users Present In SharePoint On-Premise Site - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Get All Orphan Users Present In SharePoint On-Premise Site - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"An Orphaned User is a user account that is available in SharePoint site but that user can&#8217;t access SharePoint any longer. This can be in a case if the user account is deleted or disabled from the Active Directory. In this blog, first, we are retrieving all the users from the user information list of the site collection, then we [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-25T07:13:23+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Get All Orphan Users Present In SharePoint On-Premise Site - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/","og_locale":"en_US","og_type":"article","og_title":"How To Get All Orphan Users Present In SharePoint On-Premise Site - Softree Technology","og_description":"An Orphaned User is a user account that is available in SharePoint site but that user can&#8217;t access SharePoint any longer. This can be in a case if the user account is deleted or disabled from the Active Directory. In this blog, first, we are retrieving all the users from the user information list of the site collection, then we [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/","og_site_name":"Softree Technology","article_published_time":"2019-01-25T07:13:23+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"How To Get All Orphan Users Present In SharePoint On-Premise Site","datePublished":"2019-01-25T07:13:23+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/"},"wordCount":494,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/img1-1-1024x375.png","keywords":["Get all orphan users from sharepoint onpremise site","How to get orphan users of a site collection","retrieve all orphan users of a site using csom"],"articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/","name":"How To Get All Orphan Users Present In SharePoint On-Premise Site - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/img1-1-1024x375.png","datePublished":"2019-01-25T07:13:23+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#primaryimage","url":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/img1-1-1024x375.png","contentUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/img1-1-1024x375.png"},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/how-to-get-all-orphan-users-present-in-sharepoint-on-premise-site\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Get All Orphan Users Present In SharePoint On-Premise Site"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=876"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/876\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}