locked
Need asp.net code to retrieve a specific users list of sharepoint sites they have access to RRS feed

  • Question

  • I need to write code to generate the list of sites a user has access to through sharepoint 2013. I am attempting to do this in asp.net using C#. Below is the code I have so far (just adding a little at a time and debugging). 

    <%@ Page Language="C#" %>
        using Microsoft.Office.Server.Search.Administration;
        using Microsoft.Office.Server.Search.Administration.SearchServiceApplicationProxy
        using System.ServiceProcess;
        using System.ServiceProcess.ServiceController;
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        string fac1;
        string userid;
     
        protected void btnShowInfo_Click(object sender, EventArgs e)
        {
            StringBuilder strInformation = new StringBuilder();
            try
            {
                userid = GetHTTPContext();
         string sub = userid.Substring(5);
                userid = sub;
                GetuserAccess(userid);
               
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message + "<BR>" + ex.StackTrace);
            }
            finally
            {
                strInformation = null;
            }
        }

     
        private string GetHTTPContext()
        {
           return(HttpContext.Current.User.Identity.Name);
        }


        private void GetuserAccess()
     {
                string queryText = "SELECT url, title " +
                                  "FROM Scope() " +
                                  "WHERE \"Scope\" = 'All Sites' " +
                                  "AND (ContentClass = 'STS_Site' OR ContentClass = 'STS_Web')";

               SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));       
    }

    When I try and preview it in a browser I ge the following error:

    CS0246: The type or namespace name 'SearchServiceApplicationProxy' could not be
    found (are you missing a using directive or an assembly reference?)

    What am I missing? Does anyone know where  I can find the full code for this as well?

    Thanks.  

    Thursday, June 30, 2016 1:53 PM

Answers

  • Hi,

    You need to check two things on is to add the reference of the assembly as Rahul said, you can go to your solution explorer à Reference à right click add reference à Browser à

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI and find the assembly with name Microsoft.Office.Server.Search.dll, after adding the reference you need to add the namespace on your cs file.

    Second option you check your .net framework target type it may your .Net framework version incompatibility can happen in two ways a client profile project referencing a full framework project; or an older framework version targeting a newer framework version

    you can also get this error when you create a new project in VS2012 or VS2013 (which uses .Net 4.5 as the default framework) and the referencing project(s) use .Net 4.0 (this is common when you have migrated from VS2010 to VS2012 or VS2013 and you then add a new project) here try to change the .net framework to lower version and rebuild the project.


    KRISHANA KUMAR

    SharePoint Architect

    Mosstechnet-kk.com

     

    Please click "Mark As Answer" if this post solves your problem or "Vote As Helpful" if it was useful

    • Marked as answer by Victoria Xia Monday, July 18, 2016 1:17 AM
    Monday, July 11, 2016 8:55 PM

All replies

  • Have you looked for one of the several hundred pre-existing examples of this? Try the Patterns and Practices site, I think it's got a couple of good starting points for this.
    Thursday, June 30, 2016 4:35 PM
  • I looked there but not seeing anything specifically related to pulling the sites a user has access to. Searched both the top level and under sharepoint.  
    Friday, July 1, 2016 8:44 PM
  • Hi,

    As Alex said there are many examples you can check these, Using Server object model please refer this.

    http://burnignorance.com/sharepoint-tips-and-hacks/checking-current-users-permission-on-a-sharepoint-list-using-c/

    Using client object model you can try this.

    http://sharepoint.stackexchange.com/questions/116512/sharepoint-2013-how-to-get-all-lists-a-user-has-access-to-from-provider-hosted


    KRISHANA KUMAR

    SharePoint Architect

    Mosstechnet-kk.com

     

    Please click "Mark As Answer" if this post solves your problem or "Vote As Helpful" if it was useful

    • Marked as answer by Patrick_Liang Wednesday, July 6, 2016 11:08 AM
    • Unmarked as answer by J-Bal Friday, July 8, 2016 8:08 PM
    Friday, July 1, 2016 9:39 PM
  • Thanks for the links.

    Well I though I was making some progress. I have started with the code below but now I am getting the eror the The type or namespace name 'SPUser' could not be found (are you missing a using
    directive or an assembly reference?).

    <%@ Page Language="C#" %>
        using Microsoft.Office.Server.Search.Administration;
        using Microsoft.Office.Server.Search.Administration.SearchServiceApplicationProxy
        using System.ServiceProcess;
        using System.ServiceProcess.ServiceController;
        using System.IO;
        using Microsoft.SharePoint;
        using System;
        using System.Text;
        using (SPSite spSite = new SPSite("http://portal.mfa.net"));
        using (SPWeb spWeb = spSite.OpenWeb());
        using System.Web.UI.WebControls.WebParts;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
     
        private void GetuserAccess()
           
     {
     
              SPUser currentUser = spWeb.SiteUsers[HttpContext.Current.User.Identity.Name.ToString()]);

            }

    Friday, July 8, 2016 8:13 PM
  • Hi,

    In your project, add a reference to the dll

    Microsoft.Office.Server.Search.dll

    I hope this will help you out.


    Thanks, Rahul Rashu

    Saturday, July 9, 2016 2:17 AM
  • Hi,

    You need to check two things on is to add the reference of the assembly as Rahul said, you can go to your solution explorer à Reference à right click add reference à Browser à

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI and find the assembly with name Microsoft.Office.Server.Search.dll, after adding the reference you need to add the namespace on your cs file.

    Second option you check your .net framework target type it may your .Net framework version incompatibility can happen in two ways a client profile project referencing a full framework project; or an older framework version targeting a newer framework version

    you can also get this error when you create a new project in VS2012 or VS2013 (which uses .Net 4.5 as the default framework) and the referencing project(s) use .Net 4.0 (this is common when you have migrated from VS2010 to VS2012 or VS2013 and you then add a new project) here try to change the .net framework to lower version and rebuild the project.


    KRISHANA KUMAR

    SharePoint Architect

    Mosstechnet-kk.com

     

    Please click "Mark As Answer" if this post solves your problem or "Vote As Helpful" if it was useful

    • Marked as answer by Victoria Xia Monday, July 18, 2016 1:17 AM
    Monday, July 11, 2016 8:55 PM