Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am having a strange problem in accessing the SharePoint 2013 search object model. I have a custom class, when the code is excuted in console application, it's works ok. but in wcf, the return result always 0 result.

here is the main code:

    private int SearchTest(string siteUrl, string queryText, int rowLimit, int startRow, string loginUserName)
    {
        //get current user token
        SPUserToken curUserToken = GetUserTokenByUser(siteUrl, loginUserName);

        //using (SPSite site = new SPSite(siteUrl, curUserToken))
        using (SPSite site = new SPSite(siteUrl))
        {
            KeywordQuery keywordQuery = new KeywordQuery(site);

            keywordQuery.ResultsProvider = SearchProvider.Default;
            keywordQuery.QueryText = queryText;
            keywordQuery.RowLimit = rowLimit;
            keywordQuery.StartRow = startRow;

            SearchExecutor searchExecutor = new SearchExecutor();
            ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);

            var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);

            var resultTable = resultTables.FirstOrDefault();
            //output result 
            return resultTable.TotalRows;
        }
    }

Any idea why is this happening?

the wcf service hosted in SharePoint, and call the wcf code as below:

SPSLamsServiceClient client = new SPSLamsServiceClient();

client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("userName", "password", "domain"); 

var search = client.Search(siteUrl, "test", 20, 0, "userloginname");
share|improve this question
add comment

1 Answer

Sounds like you are losing your impersonation context. Is the wcf service hosted in SharePoint?

share|improve this answer
    
yes, the wcf service hosted in SharePoint, and call the wcf code as below: SPSServiceClient client = new SPSServiceClient(); client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation; client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("userName", "password", "domain"); –  chia_liu May 23 '13 at 1:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.