Skip to main content
deleted 101 characters in body
Source Link
katbyte
  • 206
  • 1
  • 6

There is no need to write a custom trimmer in 2010 (i am not sure about 2013) as you can impersonate the user and query search as if you were her.

//elevate, impersonate and search:
using (HostingEnvironment.Impersonate()){
    using (user//HostingEnvironment.Impersonate()) {
     will not work returnin SP2013.KeywordQuery(query);
    }
};

//or
SPSecurity.RunWithElevatedPrivileges(() => { 
    using (user.Impersonate()) {
        //Search code goes here
    }
});

There is no need to write a custom trimmer in 2010 (i am not sure about 2013) as you can impersonate the user and query search as if you were her.

//elevate, impersonate and search:
using (HostingEnvironment.Impersonate()){
    using (user.Impersonate()) {
        return SP.KeywordQuery(query);
    }
};

//or
SPSecurity.RunWithElevatedPrivileges(() => {
    using (user.Impersonate()) {
        //Search code goes here
    }
});

There is no need to write a custom trimmer as you can impersonate the user and query search as if you were her.

//elevate, impersonate and search:
    //HostingEnvironment.Impersonate will not work in 2013.
SPSecurity.RunWithElevatedPrivileges(() => { 
    using (user.Impersonate()) {
        //Search code goes here
    }
});
Source Link
katbyte
  • 206
  • 1
  • 6

There is no need to write a custom trimmer in 2010 (i am not sure about 2013) as you can impersonate the user and query search as if you were her.

Helper classes:

public class WindowsImpersonation : IDisposable {

    public readonly bool impersonated = false;

    private WindowsImpersonationContext wic  = null;

    //upn is the user account in the form of [email protected]
    //ntAccount is not required but is used to prevent unnecessary impersonation
    public WindowsImpersonation(string upn, string ntAccount = "") {

        //see if impersonation is required
        WindowsIdentity current = WindowsIdentity.GetCurrent();
        if ( current != null && current.Name.Equals(ntAccount, StringComparison.OrdinalIgnoreCase) ) {
            return; //nope
        } 

        try {

            //
            // current user account must have the following permissions:
            //      - Act as Part of the Operating System
            //      - Impersonate a client after authentication
            //
            //  gpedit.msc -> local computer -> Windows Settings -> Security Settings -> Local Policies -> User Rights Assignment


            //impersonate user
            wic = (new WindowsIdentity(upn)).Impersonate();
            impersonated = true;
        } catch(Exception) {
            Dispose();
            throw;
        }

    }


    public void Dispose() {
        if (wic != null) {
            wic.Undo();
            wic = null;
        } 
    }

}

public static class Extensions_User_Impersonate {

     public static IDisposable Impersonate(this User user) {
         return new WindowsImpersonation(user.upn, user.ntAccount);
     }

}

Example:

//elevate, impersonate and search:
using (HostingEnvironment.Impersonate()){
    using (user.Impersonate()) {
        return SP.KeywordQuery(query);
    }
};

//or
SPSecurity.RunWithElevatedPrivileges(() => {
    using (user.Impersonate()) {
        //Search code goes here
    }
});

(some associated classes not included and some comments redacted)