Friday 26 August 2011

sharepoint learning site


http://msdn.microsoft.com/en-us/SP2010DevTrainingCourse.aspx

http://msdn.microsoft.com/en-us/SharePoint

70-573:
http://blog.beckybertram.com/Exams.aspx



Studying for your SharePoint 2010 (70-573) exam.

I have decided to start studying for my first SharePoint 2010 exam. If you are doing the same then these resources may help.

Thursday 25 August 2011

Getting User Profile information

/ get the BadgeNumber for this user from the profile store
string _badgeNumber = string.Empty;
SPSite _SPSite = null;
try
{
    // TODO: move this to a configuration setting somewhere
    // get a reference to the current site
    _SPSite = new SPSite("http://siteurl/");
    // get a reference to the context of the current site
    ServerContext _ServerContext = ServerContext.GetContext(_SPSite);
    // get a UserProfileManager
    UserProfileManager _UserProfileManager = new UserProfileManager(_ServerContext);
    // get the UserProfile of the logged on user
    UserProfile _CurrentUserUserProfile = _UserProfileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);
    _badgeNumber = (string)_CurrentUserUserProfile["badgeNumber "].Value;  // NOT ["Badge Number"], that was my initial mistake, using display name instead of name :)
}
finally
{
    _SPSite.RootWeb.Dispose();
    _SPSite.Dispose();
}

(or)

 object user = null;
            _SPSite = new SPSite("http://vodafonedev:24/");
            string siteURL = SPContext.Current.Site.Url;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteURL))
                {
                    ServerContext serverContext = ServerContext.GetContext(_SPSite);
                    UserProfileManager profileManager = new UserProfileManager(serverContext);
                   // SPUser spUser = site.RootWeb.Users.GetByID(id);
                    UserProfile profile = profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
                  
                    user = new
                    {
                        Account = profile["AccountName"].Value,
                        Title = profile["Title"].Value,
                        First = profile["FirstName"].Value,
                        Last = profile["LastName"].Value,
                        PreferredName = profile["PreferredName"].Value,
                        WorkPhone = profile["WorkPhone"].Value,
                        Fax = profile["Fax"].Value,
                        WorkEmail = profile["WorkEmail"].Value,
                        Office = profile["Office"].Value,
                        Manager = profile["Manager"].Value
                    };
                }
            });




Sunday 21 August 2011

Adding a value to a user field

Adding a value to a user field



This is the same, but in reverse. We use the same class (SPFieldUserValue ) and give it the values we want for the user.



using (SPSite site = new SPSite("http://portal"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Example For Users"];
                    SPListItem item = list.Items[0];

                    SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
                    item["User Name"] = userValue;
                    item.Update();
                }
            }


The image below shows the list after I updated the list item:

Get a user from a list item:

Get a user from a list item:



To get a user we use the SPFieldUserValue class, which accepts a SPWeb and a string value as parameters. The string value is the value from the list that contains the ID and the account name. Once we have that, we can use the SPFieldUserValue to get information about the user.

Example:

 using (SPSite site = new SPSite("http://portal"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Example For Users"];
                    SPListItem item = list.Items[0];

                    SPFieldUserValue userValue = new SPFieldUserValue(web, item["User Name"].ToString());
                    if (userValue.User.LoginName == web.CurrentUser.LoginName)
                    {
                        //do something!
                    }
                }
            }