Forum Discussion

Marcela Bovio's avatar
Marcela Bovio
Copper Contributor
Mar 02, 2017

SharePoint Online: import search configuration xml using app permissions

Hi,

I have a console application connecting to a SharePoint Online tenant using app permissions; the app principal has full control permissions in the whole tenant.

 

I'm trying to import a search configuration XML to a site collection, but when I do I get the following error: 

 

An unhandled exception of type 'Microsoft.SharePoint.Client.ServerException' occurred in Microsoft.SharePoint.Client.Runtime.dll

Additional information: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.

 

Here's my code:

 

Uri siteUrl = new Uri("https://{tenant}.sharepoint.com/teams/testsite");
var settingsFile = "SearchConfiguration.xml";
string realm = TokenHelper.GetRealmFromTargetUrl(siteUrl);
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUrl.Authority, realm).AccessToken;

using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(siteUrl.ToString(), accessToken)) { Web web = context.Web; context.Load(web); context.ExecuteQuery(); SearchConfigurationPortability sconfig = new SearchConfigurationPortability(context); SearchObjectOwner owner = new SearchObjectOwner(context, SearchObjectLevel.SPWeb); sconfig.ImportSearchConfiguration(owner, System.IO.File.ReadAllText(settingsFile)); context.Load(sconfig); context.ExecuteQueryRetry(); }

 

And here's the search configuration XML:

 

<SearchConfigurationSettings xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Portability">
  <SearchQueryConfigurationSettings>
    <SearchQueryConfigurationSettings>
      <BestBets xmlns:d4p1="http://www.microsoft.com/sharepoint/search/KnownTypes/2008/08" />
      <DefaultSourceId>00000000-0000-0000-0000-000000000000</DefaultSourceId>
      <DefaultSourceIdSet>true</DefaultSourceIdSet>
      <DeployToParent>false</DeployToParent>
      <DisableInheritanceOnImport>false</DisableInheritanceOnImport>
      <QueryRuleGroups xmlns:d4p1="http://www.microsoft.com/sharepoint/search/KnownTypes/2008/08" />
      <QueryRules xmlns:d4p1="http://www.microsoft.com/sharepoint/search/KnownTypes/2008/08" />
      <ResultTypes xmlns:d4p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration" />
      <Sources xmlns:d4p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration.Query">
        <d4p1:Source>
          <d4p1:Active>true</d4p1:Active>
          <d4p1:AuthInfo xmlns:d6p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration" i:nil="true" />
          <d4p1:BuiltIn>false</d4p1:BuiltIn>
          <d4p1:ConnectionTimeout>15000</d4p1:ConnectionTimeout>
          <d4p1:ConnectionUrlTemplate />
          <d4p1:CreatedDate>2017-03-02T10:16:37.267</d4p1:CreatedDate>
          <d4p1:Description />
          <d4p1:HasPermissionToReadAuthInfo>true</d4p1:HasPermissionToReadAuthInfo>
          <d4p1:Id>cdb12bed-2bb3-4099-9be2-f28cb885cafa</d4p1:Id>
          <d4p1:IndexOffset>0</d4p1:IndexOffset>
          <d4p1:LastModifiedDate>2017-03-02T10:16:37.267</d4p1:LastModifiedDate>
          <d4p1:MaximumResponseLength>64</d4p1:MaximumResponseLength>
          <d4p1:Name>Portaal</d4p1:Name>
          <d4p1:Owner xmlns:d6p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration">
            <d6p1:DatabaseId>00000000-0000-0000-0000-000000000000</d6p1:DatabaseId>
            <d6p1:DisableInheritance>false</d6p1:DisableInheritance>
            <d6p1:SPFarmId>baa1b08f-cbca-4af8-95fe-67ba9526f9b6</d6p1:SPFarmId>
            <d6p1:SPSiteId>d1befcbb-1418-46f6-b081-3f49fc647d39</d6p1:SPSiteId>
            <d6p1:SPSiteSubscriptionId>68ec952c-1c59-47b4-848c-4e41c1039ed1</d6p1:SPSiteSubscriptionId>
            <d6p1:SPWebId>8c99e281-0327-4be0-8625-b3752a0b1cf2</d6p1:SPWebId>
          </d4p1:Owner>
          <d4p1:ProviderId>fa947043-6046-4f97-9714-40d4c113963d</d4p1:ProviderId>
          <d4p1:QueryTransform xmlns:d6p1="http://www.microsoft.com/sharepoint/search/KnownTypes/2008/08">
            <d6p1:Id>e0ca6315-71f2-4db9-a073-a594039b61a2</d6p1:Id>
            <d6p1:IsSystem>false</d6p1:IsSystem>
            <d6p1:OverridePropertiesForSeralization xmlns:d7p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
              <d7p1:KeyValueOfstringanyType>
                <d7p1:Key>AllowPartialResults</d7p1:Key>
                <d7p1:Value xmlns:d9p1="http://www.w3.org/2001/XMLSchema" i:type="d9p1:boolean">true</d7p1:Value>
              </d7p1:KeyValueOfstringanyType>
            </d6p1:OverridePropertiesForSeralization>
            <d6p1:OwnerId>f68831ed-693d-4960-8263-c8038268c95b</d6p1:OwnerId>
            <d6p1:ParentId>cdb12bed-2bb3-4099-9be2-f28cb885cafa</d6p1:ParentId>
            <d6p1:ParentType>Source</d6p1:ParentType>
            <d6p1:QueryPropertyExpressions>
              <d6p1:MaxSize>2147483647</d6p1:MaxSize>
              <d6p1:OrderedItems />
            </d6p1:QueryPropertyExpressions>
            <d6p1:_IsReadOnly>true</d6p1:_IsReadOnly>
            <d6p1:_QueryTemplate>{searchTerms} Path:{SiteCollection.URL}</d6p1:_QueryTemplate>
            <d6p1:_SourceId i:nil="true" />
          </d4p1:QueryTransform>
        </d4p1:Source>
      </Sources>
      <UserSegments xmlns:d4p1="http://www.microsoft.com/sharepoint/search/KnownTypes/2008/08" />
    </SearchQueryConfigurationSettings>
  </SearchQueryConfigurationSettings>
  <SearchRankingModelConfigurationSettings>
    <RankingModels xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
  </SearchRankingModelConfigurationSettings>
  <SearchSchemaConfigurationSettings>
    <Aliases xmlns:d3p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration">
      <d3p1:LastItemName i:nil="true" />
      <d3p1:dictionary xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
    </Aliases>
    <CategoriesAndCrawledProperties xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
    <CrawledProperties xmlns:d3p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration" i:nil="true" />
    <ManagedProperties xmlns:d3p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration">
      <d3p1:LastItemName i:nil="true" />
      <d3p1:dictionary xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
      <d3p1:TotalCount>0</d3p1:TotalCount>
    </ManagedProperties>
    <Mappings xmlns:d3p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration">
      <d3p1:LastItemName i:nil="true" />
      <d3p1:dictionary xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
    </Mappings>
    <Overrides xmlns:d3p1="http://schemas.datacontract.org/2004/07/Microsoft.Office.Server.Search.Administration">
      <d3p1:LastItemName i:nil="true" />
      <d3p1:dictionary xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
    </Overrides>
  </SearchSchemaConfigurationSettings>
</SearchConfigurationSettings>

 

The same console app is able to provision site collections to the tenant, so I know the permissions are working. And if I connect to the tenant using an admin username and password then I can succesfully import the same XML file.

 

Any ideas as to why this doesn't work?

 

Thanks in advance for your help!

3 Replies

  • Hi Marcela, not 100% sure, but I'd say, the Import search configuration is not allowed in an App-Only context (kind of a bug, as the permissions are assigned correctly).

Resources