0

I m trying to create a search methode web service in SharePoint 2010 like :

i put a username as an imput(without knowing password) and a keywords to search for and the methode return all documents that containe a KeyWords that the UserName got acces to.

Using the Query werbservice, instead of searching by UserName credential(as input), i got result by credentiel of User calling the Web Service.

please can anyone help me ?! Can i do this by calling a methode in Search Web service ?

Thanks in advance !!!!

Best regards,

0

You can achieve this using SharePoint server object model. Example code is below.

SPUserToken userToken;
using(SPSite s = new SPSite(site_url))
{
    using(SPWeb w = s.OpenWeb())
    {
        //UserName comes from the request like keywords
        userToken = web.AllUsers[UserName].UserToken;
    }
} 

using(SPSite s = new SPSite(site_url, userToken))
{
    using(SPWeb w = s.OpenWeb())
    {
        //add your search logic
        //this will use passed in UserName for all SPSite and SPWeb API calls.
    }
}
  • in fact i ve tried this solution, but when i call the WS, it's still work with the credential of the user who open the IE or the Browzer instead of using the impersonate User. – Conyel Aug 24 '17 at 10:58
  • Can you make sure your code is inside the 2nd using spweb block? The code in that block will run under specified user token context. The code outside the block will run under the security context of the user who execute the web service. – Ahmad Zia Aug 24 '17 at 11:30
  • yes i ve put my code into the 2nd using, but it's still working with executing user credential. I think your code will work if i search directly in the library without using the Query Search WS. – Conyel Aug 24 '17 at 14:48
  • Sorry i thought you are searching directly in the code. You are correct, initiating a Query Search WS always use the current logged in credentials. The SPUserToken does not work in that case. You need to use the code (no web service) to impersonate user using SPUserToken. All your searching code/project can be a web service though. So when you call your web service, the code would use logged in user credentials but then SPUserToken impersonate the passed in UserName. – Ahmad Zia Aug 24 '17 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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