Showing posts with label SharePoint Object Model. Show all posts
Showing posts with label SharePoint Object Model. Show all posts

Tuesday, August 30, 2011

Removing the OOB categories from the web part tool pane

Often while developing custom web parts for SharePoint 2010 or MOSS, sometimes it is required to hide the OOB tool pane (for e.g. Appearance, Layouts or Advanced) from the end user.

Trying for hours and scratching my head, I could not find any elegant way to achieve this. Finally I decided to use a hack via reflection.

In my custom editor part class on the CreateChildControls() method, I am calling a method HideOtherToolParts. The HideOtherToolParts iterates over the control collection for this editorpart and checks if the type of the child control is Microsoft.SharePoint.WebPartPages.WebPartToolPart and accordingly hiding that child.

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                this.HideOtherToolParts(this.Parent.Controls);
                //other operations
            }
            catch
            {
                throw;
            }
        }

        private void HideOtherToolParts(ControlCollection controls)
        {
            try
            {
                foreach (Control toolPart in controls)
                {
                    if (toolPart.GetType().FullName == "Microsoft.SharePoint.WebPartPages.WebPartToolPart")
                    {
                        toolPart.Visible = false;
                    }
                }
            }
            catch
            {
                throw;
            }
        }

Hope this helps!

Wednesday, January 27, 2010

SharePoint 2010 Social Networking Part 1: Setting the user My Site status message programmatically

SharePoint 2010 comes along with a wide range of the social feature like note boards, rating, bookmarking, tagging etc. I’ll be doing a series of blog posts to share some code snippets for managing these cool features programmatically.

One of the interesting social feature is setting the My Site status message which depicts the mood of the user. In the part 1 of this series I’ll demonstrate a small code snippet to set the My Site status message programmatically.

So we’ll start by adding the required assembly references to our console application:

Microsoft.SharePoint.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.UserProfiles.dll
System.Web

Followed by the following namespace decelerations:

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

Now one wearied issue that the users with Visual Studio 2010 beta would notice here is as soon as you compile the solution, you’ll probably notice this error followed by a warning:

image

I wasted a whole day fighting this issue until I found this post by Chris where he points out it as a development issue in VS2010 beta build.

So essentially we would need to add the reference to System.Web.DataVisualization.dll into our project and we are good to go.

Now we’ll add the method as shown below to our application:

  SetStatusMessage

Essentially here we are stating by instantiating the SPSite object for a SharePoint site URL (http://intranet.contoso.com). Then we are creating an object of SPServiceContext class which would enable us make a call to the user profile service application.

Next is to instantiate the UserProfileManager class object for accessing the user profiles and their data stored in the user profile database. The GetUserProfile method of the UserProfileManager class accepts several overloads and returns the user profile for the specified user (in our case the administrator).

Now among the several OOB user profile properties configured on the server, the property SPS-StatusNotes is the one which we are looking for. Setting the value of this property with a string value would set the the status message for the user. The final step is to commit the changes by calling the Commit method.

Now call this method and pass a string message “Status set from code.”. Finally executing our console app would result in setting the status message for administrator as shown in the image below.

 My Site status.

Saturday, July 4, 2009

Overwriting Files in SharePoint using Module Element

As documented on msdn, the IgnoreIfAlreadyExists attribute in the file element provisions a file even if it already exists at the specified URL. However it doesn't works as expected.
Searching for solution on the web, I came across a fantastic blog post by John Leino where he has suggested a programmatic approach to workaround this issue.
John describes creating a feature receiver which upon activation would get all the files along with their properties from the module element. Based on these properties, the files would then be added/overwritten on their target locations.
I created a small POC using John’s approach and it really worked well !. But his code seems to break under the following conditions:
  • If you have multiple modules in your element.xml file.
  • If the target SharePoint library forces the checking and checkout policy.
So I extended John’s code to address these issues by adding/updating few methods.

Added a CheckOutStaus method to get the file checked out status:
CheckOutStaus


For publishing sites the content needs to be approved before it can be seen by other site users (e.g. on the masterpage library). So I added a method CheckContentApproval to verify that whether content approval is enabled on the target SharePoint library:
CheckContentApproval


Updated the UpdateFilesInModule method to looks like this:
UpdateFilesInModuleUpdateFilesInModule
So in the above method after from getting all the files and their properties from the module, we are doing two thing
  1. Getting the checkout status of the file.
  2. Checking whether content approval is enabled on the target SharePoint library and accordingly approving the file.
The entire source code can be downloaded from .