Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts

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.

Tuesday, May 18, 2010

Add a WebPart into the Rich Content of a wiki page in SP2010

After a long struggle looking in the SP API for how to insert a WebPart in the "rich content" of a wiki page, i found the answer. Thanks to Stefan Stanev for posting the answer to my question.

I have modified the code a bit to suite my needs

SPSite osite = properties.Feature.Parent as SPSite; 
   using (SPWeb oWeb = osite.OpenWeb(osite.RootWeb.ID)) 
  {
    //SPLimitedWebPartManager webpartsManager =  oWeb.GetLimitedWebPartManager(oWeb.RootFolder.WelcomePage, PersonalizationScope.Shared);

   SPFile file = oWeb.GetFile(oWeb.RootFolder.WelcomePage);
   Guid cwpID = AddWebPartToRichContent(file);
   Guid xlvID = AddListWebPartToRichContent(file, "Announcements");

  }

private Guid AddWebPartToRichContent(SPFile file)
{
// NOTE: we assume that the SPFile was checked out
// get the limited wp manager for that SPFile
SPLimitedWebPartManager mngr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

// create a sample content editor wp
ContentEditorWebPart cwp = new ContentEditorWebPart();
cwp.Title = "My editor";

// add the web part to the special wpz zone
mngr.AddWebPart(cwp, "wpz", 0);

// store the web part's internal ID
Guid cwpID = mngr.GetStorageKey(cwp);

// get the list item for that SPFile
SPListItem item = file.Item;

// update the PublishingPageContent field value
item["WikiField"] += this.GetEmbeddedWPString(cwpID);

// update the item
item.SystemUpdate(false);

// the SPFile instance can be checked in, published, approved afterwards

return cwpID;

}

private Guid AddListWebPartToRichContent(SPFile file, string listTitle)
{
// NOTE: we assume that the SPFile was checked out
// get the limited wp manager for that SPFile
SPLimitedWebPartManager mngr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

// create a sample XLV
XsltListViewWebPart xlv = new XsltListViewWebPart();

// get a target list
SPList list = file.ParentFolder.ParentWeb.Lists[listTitle];

// set the ListName property with the capitalized list ID w/o the curly braces
xlv.ListName = list.ID.ToString("B").ToUpper();

// use the schema of the default view
xlv.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();

// add the web part to the special wpz zone
mngr.AddWebPart(xlv, "wpz", 0);

// store the web part's internal ID
Guid xlvID = mngr.GetStorageKey(xlv);

// get the list item for that SPFile
SPListItem item = file.Item;

// update the PublishingPageContent field value
item["WikiField"] += this.GetEmbeddedWPString(xlvID);

// update the item
item.SystemUpdate(false);

// the SPFile instance can be checked in, published, approved afterwards

return xlvID;

}

private string GetEmbeddedWPString(Guid wpID)
{
const string form = @"
                                    

                                    

                                    

                                    
                                   
";

// set the web part's ID as part of the ID-s of tho div elements
return string.Format(form, wpID);

}


I hope this would help someone out there.

Update: Found another helpful post
http://www.habaneros.com/mobile/blog/10-03-30/Programmatically_Change_Content_on_a_Wiki_Page_in_SharePoint_2010.aspx

Saturday, April 17, 2010

SharePoint 2010 reaches RTM!

Today SharePoint 2010 RTM was released. This is an exciting milestone for Microsoft. Volume License customers will be able to download SharePoint on the 27th of April if they have an active SA. Customers who do not have the SA will be able to download it on 1st of May.

For more information, check the Microsoft SharePoint Team Blog