Specify a Search Scope When Using search.asmx Service

This article will outline how to setup your code:  link

For my app, I needed to leverage a search scope as part of my query.  Note: when setting up the XML packet, pay close attention to your spacing and closing tags.  If not, you will run into endless error’s that have little to no relevant description.

Example:

<ResponsePacket xmlns="urn:Microsoft.Search.Response"><Response domain="QDomain"><Status>ERROR_BAD_REQUEST</Status><DebugErrorMessage>Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 442.</DebugErrorMessage></Response></ResponsePacket>

Here is my code example. Keep in mind that I’ve already setup my web reference (_vti_bin/Search.asmx).

                var webServ = "http://rootSiteUrl/mysitecolleciton/_vti_bin/Search.asmx";

                QueryService qService = new QueryService();
                qService.Credentials = System.Net.CredentialCache.DefaultCredentials;
                qService.Url = webServ;

                SearchQuery sQuery = new SearchQuery();
                var querySetup = sQuery.SearchString("my search value");
                var queryResults = qService.QueryEx(querySetup);

    class SearchQuery
    {
        public string SearchString (string entityId )
        {
            StringBuilder queryXml = new StringBuilder();
            queryXml.Append("<QueryPacket xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000\">");
            queryXml.Append("<Query domain=\"QDomain\">");
            queryXml.Append("<SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format>");
            queryXml.Append("</SupportedFormats>");
            queryXml.Append("<Context>");
            queryXml.Append("<QueryText language='en-US' type='MSSQLFT'>");
            queryXml.Append("SELECT Title, Path, Description, Write, Rank, Size FROM Scope() WHERE \"Scope\" = 'Big - Scope' AND CONTAINS(owsOrderx0020Number,'" + entityId +"')");
            queryXml.Append("</QueryText>");
            queryXml.Append("</Context>");
            queryXml.Append("</Query>");
            queryXml.Append("</QueryPacket>");

            return queryXml.ToString();
        }
    }

Error When Using PowerShell to Call SharePoint Web Service

New-WebServiceProxy : The HTML document does not contain Web service discovery information.
At C:\myScript.ps1:91 char:17
+ ...    $Service = New-WebServiceProxy -UseDefaultCredential -uri $webServ
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (http://mySharePoint....bin/Search.asmx:Uri) [New-WebServiceProxy], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.NewWebServiceProxy

First attempt at calling the search.asmx service.

$webServ = "http://mySharePointsite.com/_vti_bin/Search.asmx"
$Service = New-WebServiceProxy -UseDefaultCredential -uri $webServ
[xml]$Results = $Service.Query($QueryPacket.OuterXml)

After adding ?wsdl to the uri string, it worked.

$webServ = "http://mySharePointsite.com/_vti_bin/Search.asmx?wsdl"
$Service = New-WebServiceProxy -UseDefaultCredential -uri $webServ
[xml]$Results = $Service.Query($QueryPacket.OuterXml)

Use UserGroup.asmx to get all the users in a SharePoint site

This is a little tricky. I was looking for a way to list all the users that you see in Site Settings –> People and Groups, but I found more info than I needed. This post will be updated once I can track down the actual site users.

The script uses the Users and Groups web service to pull all the users in the site collection.
UserGroup.asmx

In my case, the LoginName is setup like this: domain\username

First try:

$webServ = "http://sharepointed/sites/MySiteCollection/_vti_bin/UserGroup.asmx"
$Service = New-WebServiceProxy -UseDefaultCredential -uri $webServ 
$Users = $Service.GetUserCollectionFromSite().Users 
$UserNames = New-Object System.Collections.Generic.List[System.Object]

$Users.User | ForEach-Object {
	$spUser = $_.LoginName.Split('\')[1]
	$UserNames.Add($spUser)
}

Found the answer to my question. Using the Lists.asmx service, I was able to query the UserInfo list for all the site users.

$webServ = "http://sharepointed/sites/MySiteCollection/_vti_bin/Lists.asmx"
$Service = New-WebServiceProxy -UseDefaultCredential -uri $webServ 

$UserNames = New-Object System.Collections.Generic.List[System.Object]

$listname = 'UserInfo'
$listItems = $Service.GetListItems($listname, $null, $null, $null, $null, $null, $null)

for ($counter = 0;$counter -lt $listItems.data.row.Count;$counter++)
{
	$UserNames += $listItems.data.row[$counter].ows_Name
}

Using c# to get at the information. Here I output the LoginName (domain\userName).

            #create a Web Reference to http://yoursiteURL/_vti_bin/usergroup.asmx?wsdl
            #in my case, i named the reference wsUsersGroups
            wsUsersGroups.UserGroup _WSUsersGroups = new wsUsersGroups.UserGroup();
            _WSUsersGroups.Url = "http://sharepointSite/sites/SiteCollectionName/_vti_bin/usergroup.asmx";
            _WSUsersGroups.Credentials = System.Net.CredentialCache.DefaultCredentials;
            XmlNode ndUsers = _WSUsersGroups.GetAllUserCollectionFromWeb();

            StringReader rdrGroups = new StringReader(ndUsers.OuterXml);
            DataSet dsGroups = new DataSet();
            dsGroups.ReadXml(rdrGroups);

            StringBuilder sb = new StringBuilder();

            foreach (DataRow item in dsGroups.Tables[1].Rows)
            {
                sb.AppendLine(item[3].ToString());
            }

            File.WriteAllText("C:\\Users\\myname\\Desktop\\siteUSers.csv", sb.ToString());