Is it possible to create web part with Sharepoint APIs that will trim search results like this. I would need similar thing to check does user have edit permission on documents retreived from search results.

I guess other option would be developing post trimmer (ISecurityTrimmerPost)?

  • 1
    E.g. by using SharePoint's REST API the returns based on your query are automatically security trimmed. You mention using a web part, and e.g. the Content Query Web Part does this too automatically as it's based on the built-in search. Not too sure what kind of web part you are after. – moe Jul 25 '17 at 15:30
  • Yes I'm aware that results are security trimmed but I need from search to return only documents with edit permissions. I've tried crawling "shared with" but it doesn't contain all users because inheritance. – malibeg Jul 26 '17 at 7:51

You can't do it with Search or the query API. A user just needs view rights to an item in SharePoint for the results to show. You would need to develop a solution (probably using an elevated account) to enumerate the items and determine if the current user has Edit rights.

|improve this answer|||||
  • Thanks. Link that I've added to post tells me it is achivable. I've played with CSOM SearchExecutor and I get Search results that contain document urls and bunch of other properties (similar properties are returnd by Search results web part). Now I need to get SPListItem from document url. I will try this with SP API not CSOM. – malibeg Jul 26 '17 at 8:02
  • I edited my post, would it be possible to check user permission level in ISecurityTrimmerPost ? – malibeg Jul 26 '17 at 8:07
  • Sure, if you have the option to write a custom security trimmer. You cannot do this in SPO only SharePoint on-prem. – Matthew McDermott Jul 26 '17 at 22:22

I've managed to make console app with CSOM to get permissions for documents with code blow but to check permissions I need full rights or I get access denied. I will need to move this code to web part and check permissions with elevated rights.

public static void Search()
        using (ClientContext clientContext = new ClientContext("http://server.company.com"))
            KeywordQuery keywordQuery = new KeywordQuery(clientContext);
            keywordQuery.QueryText = ".docx";
            // Title,SiteName,SiteId,WebId,Path,contentclass,isdocument
            SearchExecutor searchExecutor = new SearchExecutor(clientContext);

            ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);

            foreach (var resultRow in results.Value[0].ResultRows)
                    Console.WriteLine("{0}: {1}", resultRow["Title"], resultRow["Path"]);
                    Web web = clientContext.Site.OpenWebById(new Guid(resultRow["WebId"].ToString()));


                    if (web != null)
                        var url = web.Url;
                        var relurl = web.ServerRelativeUrl;
                        File f = web.GetFileByServerRelativeUrl((relurl.Length > 1 ? relurl : "") + resultRow["Path"].ToString().Remove(0, url.Length));

                        if (f != null)
                            ListItem item = f.ListItemAllFields;
                            var permissions = item.GetUserEffectivePermissions(web.CurrentUser.LoginName);

                            foreach (var permission in Enum.GetValues(typeof(PermissionKind)).Cast<PermissionKind>())
                                var permissionName = Enum.GetName(typeof(PermissionKind), permission);
                                var hasPermission = permissions.Value.Has(permission);
                                Console.WriteLine("Permission: {0}, HasPermission: {1}", permissionName, hasPermission);

                catch (Exception ex)
|improve this answer|||||

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.