1
votes

I am quite stumped with this. In enhancing an existing feature to a SharePoint solution, I found that they were querying the Wss_Content directly. Knowing I should not be using that stored procedure, I used the SharePoint object model to retrieve the users group information from the userName. What's burning me is that it is slower then the stored procedure. Is there a smarter/faster way to get this information? We are using SharePoint 2007. Below is roughly what the function does:

private string GetTitle(string userName)
    {
        string results = string.Empty;
        try
        {                
            SPSite spSite = new SPSite("http://devvm");
            SPWeb spWeb = spSite.AllWebs[""];
            SPUser user = spWeb.AllUsers["aspnetsqlmembershipprovider:" + userName];
            SPGroupCollection groups = user.Groups;
            results = groups[0].Name;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to find user" + userName);
            results = "No Group Found";
        }
        return results;
    }

and the Stored Procedure code is:

SELECT @role=Groups.Title
    FROM WSS_Content.DBO.UserInfo  Info
    LEFT JOIN WSS_Content.DBO.GroupMembership Membership 
    ON Info.tp_ID=Membership.MemberID
    LEFT JOIN Wss_Content.DBO.Groups Groups 
    ON Membership.GroupID=Groups.ID
    WHERE tp_Login='aspnetsqlmembershipprovider:' + @username

FYI the reason I have this in a try-catch is that this is a sub-query to another list that might not have a user associated to the item. Any help in this would be greatly appreciated.

1

1 Answers

1
votes

How are you calling that code? If you are using this inside a console application then yes it will be slower, it needs to fire up the supporting objects that are normally live when using this inside a native sharepoint context.

Also you are referencing an SPSite and SPWeb object without disposing of the object, this has inherent performance issues as well.