Wednesday, June 29, 2011

Finding Page Layouts and Master Page usages


Recently, I messed up my page layout by manually editing the layout directly from the gallery. It started adding multiple web part instances all of a sudden when a page created with the page layout. So I decided to delete the layout and re-deploy the solution again.

I was getting the below error when I was trying to delete that particular page layout from my site collection's Master page and page layouts collection. (http://spsiteurl/_catalogs/masterpage/Forms/AllItems.aspx)

And I have deleted all the instances where ever the layout was being used. I was sure I was missing some place but how do I find that out??




After a long search I found a place where to get all the usages of page layouts. I went to  the Site Content and Structure by selecting "Manage Content and Structure" from the "Site Actions" menu. And navigated to the "Master Page Gallery" list in the root web.



We do have an option in the menu on the right side pane to show the related resources "Show Related Resources" to show/hide the related resources. Turning on this option and selecting the particular page layout or master page would show you the resources using the particular layout/master page.

A preview ...


Isn't this handy & cool??

Monday, June 13, 2011

Check-in & Check-out using ECMA script in SharePoint 2010

Recently, I had to enable editing just the content (not web part zones) of a publishing page by end users without the ribbon. I decided to make use of the ECMA script model to empower end users to update just the content of the page.

Provide buttons for different operations of a publishing page (edit, save and cancel) and handle the events in javascript coding and some jQuery.

I came across Sohel's blog with a pretty good description of how to make use of the ECMA script. It is basically, all the updates need to be set in its corresponding javascript objects and then "executeQueryAsync" must be called on the ClientContext object to save everything on to the server.

One porblem I faced was updating a particular field content in the current list item being edited. After doing some research on MSDN, I came across this function SP.File.listItemAllFields on MSDN (http://msdn.microsoft.com/en-us/library/ee557727.aspx) and started down that path.

I was able to check-out the current PublishingPage, update the corresponding list item and publish the page using current user's permissions.

function checkOut(){
        var ctx = SP.ClientContext.get_current();
        var page = ctx.get_web().getFileByServerRelativeUrl(window.location.pathname);
        page.checkOut();
        ctx.load(page);
        ctx.executeQueryAsync(Function.createDelegate(this, checkOut_Success),
                                            Function.createDelegate(this, checkOut_Fail));
}
function checkOut_Success(sender, args){
        //Do all sort of jquery to insert your own RTE control to edit the page contents
}
function checkOut_Fail(sender, args){
       //Do some error checking
}

function checkIn(){
         var ctx = SP.ClientContext.get_current();
        var web = ctx.get_web();
        var page = web.getFileByServerRelativeUrl(window.location.pathname);

        if (contentEditorNicInstance != null) {
            var listItem = page.get_listItemAllFields();
            listItem.set_item("PublishingPageContent", "{Updated content from the UI}");
            listItem.update();
        }
        page.checkIn();
        page.publish();
        ctx.executeQueryAsync(Function.createDelegate(this, checkIn_Success),
                                            Function.createDelegate(this, checkIn_Fail));
}
function checkIn_Success(sender, args){
       //This is to refresh the page after successful publishing with the new updated content.
      //If the page is not refreshed, the ECMA script will not support further check-in's and comes back with an error as "version conflict"
       window.location = window.location;
}
function checkIn_fail(sender, args){
      //Handle the error message
}

I think I am helping myself to remember this in future and also few others with their coding.

Senthil


Wednesday, May 25, 2011

Creating multi level discussion groups programmatically

Recently, I was required to create a multiple level discussion groups.

As we know, Sharepoint 2010 offers only one level of Discussion group (at least as far as I know) i.e., First level is Discussion Group and the inner level is Discussion Threads (individual posts).


What did I do, I create the discussion group programmatically and move the created group under an existing discussion group. This way, I was able to achieve multiple levels of discussion groups.

 var web = SPContext.Current.Web;    
 var discussionList = web.Lists[DISCUSSION_LIST_NAME];
 var discussionContentType = 
web.AvailableContentTypes[DISCUSSION_CONTENT_TYPE_NAME];
 
web.AllowUnsafeUpdates = true;
  
 var newDiscussionItem = SPUtility.CreateNewDiscussion(discussionList, title);
 newDiscussionItem["Title"] = title;
 newDiscussionItem["ContentTypeId"] = discussionContentType.Id;
 newDiscussionItem["ContentType"] = discussionContentType.Name;
newDiscussionItem.Update();
 
 //if the discussion group is a child discussion (Thread) 
 //then move the discussion group 
 //to a sub level (under its parent discussion group)
 if (parentDiscussionID != -1)
 {
    var parentDiscussionGroup = discussionList.GetItemById(parentDiscussionID);
    var destinationUrl = parentDiscussionGroup.Url + "/" 
+ newDiscussionItem.Folder.Name;
    newDiscussionItem.Folder.MoveTo(destinationUrl);
 }
 web.AllowUnsafeUpdates = false;
 
 
Using the above method of .Folder.MoveTo() any SPListItem can be moved to any destination. Hope this would help someone else.

Friday, May 6, 2011

Save a PublishingPage programmatically

We are trying to do edit and save page functionality for a publishing page programmatically. I was facing an issue where the page content is not getting updated when check-in the page programmatically. As we know the below code checks in and publishes any SharePoint item into the library.

SPContext.Current.ListItem.File.CheckIn("Page edited at: " + DateTime.Now,  
                                                SPCheckinType.MajorCheckIn);
SPContext.Current.ListItem.File.Publish("Published page at: " + DateTime.Now);

When I run the above code, the page gets checked in and gets published but the content is not getting updated in the page. To achieve that, I had to add the below code to save the contents of the page before check-in and publish the SharePoint item.

Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext.Current, 
                                         false, "Saved at: " + DateTime.Now);

This one works fine for me. Hope this helps some one else too.

Monday, April 4, 2011

Creating a Taxonomy (Managed Metadata) field (SiteColumn) using definition xml

I was in a situation where I have to create a taxonomy bound site column for our SharePoint 2010 site. First I found it little difficult but SPCAMLEditor gave me a hand in figuring out the Taxonomy field structure in the SharePoint site. One Taxonomy field uses a set of two different fields to store the value.

When I need to create a SiteColumn named "TestColumn", a taxonomy column needs two different fields (TestColumn and TestColumn_0) for the field to work properly

TestColumn is of type "TaxonomyFieldType" and TestColumn_0 is of type "Note". The "TestColumn" references the TestColumn_0 in its field definition as a TextField.

I created the FieldDefinitions by copying the Schema xml to my project as a field definition xml file. When the field definition is deployed to a test server, the field is not saving any of the selected values. It allowed me to select the values but the values are not getting stored. After spending some time, I found that the Schema definition for the "TestColumn" references the ListId and WebId that it was created from in my local machine. Once I removed those references, it started working fine.



















Hope this is helping some one else.

Senthil Subramanian