1

I need to run some code when a user login in SharePoint. I have the following code

using (SPSite oSiteCollection = new SPSite("http://mySite/sites/test"))
        {
            SPWeb oWebsite = oSiteCollection.OpenWeb();
            SPFolder oFolder = oWebsite.RootFolder;

            string groupName = "ES HR";
            var spGroup = oWebsite.Groups[groupName];
            isMember = oWebsite.IsCurrentUserMemberOfGroup(spGroup.ID);


            oFolder.WelcomePage = "SitePages/General/Dashboard.aspx";
            oFolder.Update();
            oWebsite.Dispose();
        }

First I want to know if current user is in ES HR group i want to change the homepage and my code is working for this requirement.

But where should I put this code? Do I have to put it in a feature, or do I have to put it elsewhere.

I was thinking to put my code when a user login in sharepoint

  • Are you working on SharePoint Online or SharePoint On-Premises ? – Yayati Feb 14 '17 at 10:29
  • SharePoint On-Premises – Nderon Hyseni Feb 14 '17 at 10:32
1

Create a visual web part and put following code in .cs file of your webpart. Here I have also modified your code.

The line oFolder.WelcomePage = "SitePages/General/Dashboard.aspx"; will set the welcome page for all users of the site therefore instead of setting Welcome page based on current user group, you should redirect user to the specific page if he/she is in a particular group.

protected void Page_Load(object sender, EventArgs e)
    {
        using (SPSite oSiteCollection = new SPSite("http://mySite/sites/test"))
        {
            SPWeb oWebsite = oSiteCollection.OpenWeb();
            SPFolder oFolder = oWebsite.RootFolder;

            string groupName = "ES HR";
            var spGroup = oWebsite.Groups[groupName];
            if (oWebsite.IsCurrentUserMemberOfGroup(spGroup.ID))
            {
                Response.Redirect("~/SitePages/General/Dashboard.aspx");
            }
            oWebsite.Dispose();
        }
    }

Now insert this web part into your current welcome page.

  • Response does not exist in my client context – Nderon Hyseni Feb 14 '17 at 12:34
  • Are you using .NET Framework client object model (CSOM) or Server Side Object Model Code in your code?? because I have given SSOM code in my answer. – Yayati Feb 14 '17 at 12:47
  • I think i am using ssom – Nderon Hyseni Feb 14 '17 at 12:54
  • 1
    use sputility.redirect() for redirection. SPUtility.Redirect("cnn.com", SPRedirectFlags.DoNotEndResponse | SPRedirectFlags.Trusted, HttpContext.Current); You could probably omit 'SPRedirectFlags.DoNotEndResponse'. or HttpContext.Current.Response.Redirect() . – SaMolPP Feb 14 '17 at 13:38
  • 1
    If you are using SSOM then Response object should be available via System.Web.UI namespace. Include it your .cs file. I have tried this code and its working perfectly. – Yayati Feb 14 '17 at 14:15
1

If you are working on SharePoint on-prem you can create a user control add your code to it and add it to your custom master page, so when a user will login to SharePoint this code will execute.

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.