Friday, February 18, 2011

If you ever wondered how you can do this “without code” J you can do it by placing two WebParts on a SP page; an InfoPath Form WebPart and a Query String Filter WebPart. You then make a connection between the two passing the value from the Query String Filter to the InfoPath form WebPart.
Note the following:
  • This will not work for “lookup” fields if you use the default SP list InfoPath form (That’s Why I thought you can’t do it originally for lookups). However, you can expose the “lookup” dropdown field if you create a new InfoPath form using the method below.
  • Make sure you add the “field” in your InfoPath to the list of allowable fields to be used in connections.



References:

And here is how you can do it with Code behind.

Friday, January 21, 2011

Retrieve Activities from the SharePoint 2010 Activity Feeds

I've recently worked on a webpart that will let you retrieve activities for the logged in user and display them using the default template of the activity.

The webpart gets an instance of the ActivityManager object and the UserProfile object of the logged in user. After you get the reference, you can then retrieve the activity events using the ActivityManager.GetActivitiesByUser() method. Then you retrieve the resource (.resx) file corresponding to that particular ActivityEvent replacing the values with the ActivityEvent values as shown the code below


            string userName = Environment.UserDomainName + "//" + Environment.UserName;
            SPServiceContext currentContext = SPServiceContext.GetContext(SPContext.Current.Site);

            //Get the UserProfileManager from SPServiceContext.
            UserProfileManager userProfMan = new UserProfileManager(currentContext);

            //Get the current user.
            UserProfile currentUser = userProfMan.GetUserProfile(userName);

            //Get the ActivityManager from the user and context.
            ActivityManager activityMan = new ActivityManager(currentUser, currentContext);

            ActivityEventsCollection eventsCollection = activityMan.GetActivitiesForMe(10);

            foreach (ActivityEvent activity in eventsCollection)
            {
                ActivityType activityType = activityMan.ActivityTypes[activity.ActivityTypeId];
                ActivityTemplate activityTemplate = activityType.ActivityTemplates[bool.FalseString];
                string templateStr = SPUtility.GetLocalizedString(activityTemplate.TitleFormatLocStringResourceFile, "$Resource:" + activityTemplate.TitleFormatLocStringName, (uint)CultureInfo.CurrentUICulture.LCID);
              
                //The line below returns null for the "Publisher".
                //HOWEVER, If i step through the code in debug mode, the values get populated eventually
                templateStr = templateStr.Replace("{Publisher}", activity.Publisher.Name);

                //Then you will need to output the templateStr to the UI.
                //EncodedLiteral.Text += templateStr
            }


What you might notice is that you will get an "Null" Exception when you query the Publisher and the Owner properties of the ActivityEvent object. Stepping through the code, you will notice that the values will eventually load, and you will not get the exception. I'm not sure why this happens. If anyone knows, Feel free to leave a comment. A solution for this would be to get the TemplateVariable property and parse it as an XmlDocument object and then use that to get the Event values.