简体   繁体   中英

C# Sharepoint Get current user under whitch code running

I have code from tutorial, code running on WCF service under DOMAIN\\AppPoolAccount

But code except that Access Denied, how determinate user?

using (SPSite site = new SPSite("http://portal.local"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);


                UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;

                // Retrieve all properties for the "UserProfile" profile subtype,
                // and retrieve the property values for a specific user.
                ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
                UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
                IEnumerator<ProfileSubtypeProperty> userProfileSubtypeProperties = subtypePropMgr.GetEnumerator();
                while (userProfileSubtypeProperties.MoveNext())
                {
                    string propName = userProfileSubtypeProperties.Current.Name;
                    ProfileValueCollectionBase values = userProfile.GetProfileValueCollection(propName);
                    if (values.Count > 0)
                    {

                        // Handle multivalue properties.
                        foreach (var value in values)
                        {
                            Console.WriteLine(propName + ": " + value.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine(propName + ": ");
                    }
                }
                Console.WriteLine("Press Enter to change the values.");
                Console.Read();

                // Change the value of a single-value user property.
                userProfile[PropertyConstants.Department].Value = "SDDDD";
                userProfile.Commit();


            }

How determinate user?

Use SPWeb.CurrentUser property to determine the current user:

using (var site = new SPSite(siteUrl))
{
    using (var web = site.OpenWeb())
    {
        var currentUser = web.CurrentUser;
        //..

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM