{"id":806,"date":"2018-12-07T11:55:48","date_gmt":"2018-12-07T11:55:48","guid":{"rendered":"http:\/\/blog.softreeconsulting.com\/?p=806"},"modified":"2018-12-07T11:55:48","modified_gmt":"2018-12-07T11:55:48","slug":"wildcard-search-users-using-csom-sharepoint","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/","title":{"rendered":"Wildcard search of Users using CSOM in SharePoint"},"content":{"rendered":"<p>In this blog, I will explain how to find users using ClientPeoplePickerSearchUser class. There are many ways in CSOM to get the users from SharePoint site or web but I did not find any proper way to perform wildcard search of users from SharePoint site using CSOM. For e.g lets, I want to find all users present in site collection where the first name starts with &#8220;John&#8221; or last name starts with &#8220;Patt&#8221;.<\/p>\n<p>This below-mentioned code sample will help us to perform both wildcard search and explicit search as well. The output will be matching result whether it may be a collection of users or a single user.<\/p>\n<p><strong>The script as follows:-<\/strong><\/p>\n<p>[code lang=&#8221;c&#8221;]<\/p>\n<p>using Microsoft.SharePoint.Client;<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Linq;<br \/>\nusing System.Security;<br \/>\nusing System.Text;<br \/>\nusing System.Threading.Tasks;<\/p>\n<p>namespace FindUserDetailsByPeoplepickerQuery<br \/>\n{<br \/>\nclass Program<br \/>\n{<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\nstring userToFind = &#8220;userToFind&#8221;; \/\/it may be full or partial name of the user<br \/>\nstring webUrl = &#8220;&lt;site url&gt;&#8221;;<br \/>\nstring userName = &#8220;&lt;user name&gt;&#8221;;<br \/>\nstring password = &#8220;&lt;password&gt;&#8221;;<br \/>\nSecureString passWord = new SecureString();<\/p>\n<p>try<br \/>\n{<br \/>\n\/\/creating the context of the SharePoint site<br \/>\nusing (ClientContext clientCtx = new ClientContext(webUrl))<br \/>\n{<br \/>\nforeach (char c in password.ToCharArray())<br \/>\npassWord.AppendChar(c);<\/p>\n<p>clientCtx.Credentials = new SharePointOnlineCredentials(userName, passWord);<br \/>\nclientCtx.ExecuteQuery();<\/p>\n<p>\/\/preparing the search query options<br \/>\nMicrosoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters peoplePickerQuery =<br \/>\nnew Microsoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerQueryParameters();<\/p>\n<p>peoplePickerQuery.AllowEmailAddresses = true;<br \/>\npeoplePickerQuery.AllowMultipleEntities = false;<br \/>\npeoplePickerQuery.ForceClaims = false;<br \/>\npeoplePickerQuery.MaximumEntitySuggestions = 60;<br \/>\npeoplePickerQuery.PrincipalType = Microsoft.SharePoint.Client.Utilities.PrincipalType.All;<br \/>\npeoplePickerQuery.PrincipalSource = Microsoft.SharePoint.Client.Utilities.PrincipalSource.All;<br \/>\npeoplePickerQuery.QueryString = userToFind;<br \/>\npeoplePickerQuery.AllUrlZones = true;<\/p>\n<p>peoplePickerQuery.SharePointGroupID = 0;<br \/>\npeoplePickerQuery.WebApplicationID = new Guid(&#8220;00000000-0000-0000-0000-000000000000&#8221;);<\/p>\n<p>\/\/Using peoplepicker webservice and searchquery getting the matched userInformations from SharePoint<br \/>\nClientResult&lt;String&gt; resultData =<br \/>\nMicrosoft.SharePoint.ApplicationPages.ClientPickerQuery.ClientPeoplePickerWebServiceInterface.ClientPeoplePickerSearchUser(clientCtx, peoplePickerQuery);<br \/>\nclientCtx.ExecuteQuery();<\/p>\n<p>if (resultData != null)<br \/>\n{<br \/>\nObject[] queryMatchesFound = (Object[])clientCtx.ParseObjectFromJsonString(resultData.Value);<\/p>\n<p>for (var i = 0; i &lt; queryMatchesFound.Length; i++)<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nDictionary&lt;string, object&gt; obj = (Dictionary&lt;string, object&gt;)queryMatchesFound[i];<\/p>\n<p>try { string loginName = obj[&#8220;Key&#8221;].ToString(); } catch { } \/\/this will give the loginName of the user<br \/>\ntry { string email = obj[&#8220;Description&#8221;].ToString(); } catch { } \/\/this will give the email of the user<br \/>\ntry { string Title = obj[&#8220;DisplayText&#8221;].ToString(); } catch { } \/\/this will give the title\/DisplayName of the user<br \/>\ntry { string entityType = obj[&#8220;EntityType&#8221;].ToString(); } catch { } \/\/this will give the type of the object whether it is a group or user<\/p>\n<p>}<br \/>\ncatch { }<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\ncatch { }<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>In this example, I am getting the values in JsonString format. Then I am converting it to object and by looping the collection we can get the required info about the users like loginname, displayname, email etc.<\/p>\n<p>In this blog, I have explained have you can get multiple users based on wildcard search based on either start name or last name or email id or with a partial name. Hope this information will help you out in a few scenarios.<\/p>\n<p><strong>This solution is brought to you by our SharePoint professionals.<\/strong><\/p>\n<p><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.<\/p>\n<p>Be 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<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, I will explain how to find users using ClientPeoplePickerSearchUser class. There are many ways in CSOM to get the users from SharePoint site or web but I did not find any proper way to perform wildcard search of users from SharePoint site using CSOM. For e.g lets, I want to find all users present in site collection [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[129],"tags":[193,194,195,196],"class_list":["post-806","post","type-post","status-publish","format-standard","hentry","category-all","tag-get-users-from-sharepoint-using-csom","tag-get-users-using-csom","tag-search-user-based-on-display-name-using-csom","tag-wildcard-search-of-users-in-sharepoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Wildcard search of Users using CSOM in SharePoint - 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\/all\/wildcard-search-users-using-csom-sharepoint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Wildcard search of Users using CSOM in SharePoint - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"In this blog, I will explain how to find users using ClientPeoplePickerSearchUser class. There are many ways in CSOM to get the users from SharePoint site or web but I did not find any proper way to perform wildcard search of users from SharePoint site using CSOM. For e.g lets, I want to find all users present in site collection [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-07T11:55:48+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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Wildcard search of Users using CSOM in SharePoint - 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\/all\/wildcard-search-users-using-csom-sharepoint\/","og_locale":"en_US","og_type":"article","og_title":"Wildcard search of Users using CSOM in SharePoint - Softree Technology","og_description":"In this blog, I will explain how to find users using ClientPeoplePickerSearchUser class. There are many ways in CSOM to get the users from SharePoint site or web but I did not find any proper way to perform wildcard search of users from SharePoint site using CSOM. For e.g lets, I want to find all users present in site collection [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/","og_site_name":"Softree Technology","article_published_time":"2018-12-07T11:55:48+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Wildcard search of Users using CSOM in SharePoint","datePublished":"2018-12-07T11:55:48+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/"},"wordCount":575,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"keywords":["Get users from SharePoint using csom","Get users using csom","Search user based on display name using csom","Wildcard search of users in SharePoint"],"articleSection":["All"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/","url":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/","name":"Wildcard search of Users using CSOM in SharePoint - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"datePublished":"2018-12-07T11:55:48+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/all\/wildcard-search-users-using-csom-sharepoint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Wildcard search of Users using CSOM in SharePoint"}]},{"@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\/806","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=806"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/806\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}