Showing posts with label Layouts Pages. Show all posts
Showing posts with label Layouts Pages. Show all posts

Tuesday, May 29, 2012

Manage SPListItem publishing programatically

Recently, I had to work on converting a regular SPList to have similar to a publishing feature. Like, have an item edited, review it and then publish the page for public.

I made the below modifications to the list settings to enable versioning of list items.
Go to the List Setting page and select "Versioning Settings" and set the options to the same as below.

 
This enables the list items in the list to be version controlled. The author can save a version of the list item, review it and publish it at any point of time or revert to its previously published version.

I wanted to do it programatically using a layouts page (application page) since we are using the application pages as the default display form of the particular content type.

For saving a new SPListItem, the code should look similar to this. This will save the list item content and assign the status to "Pending".

var item = SPContext.Current.ListItemitem["Title"] = SPHttpUtility.ConvertSimpleHtmlToText(this.txtTitle.Text, 255);
//this will set the status of the current item to "Pending"
item.Update();


To know how to get the CurrentListItem in an app page, please see my other post here

To publish the list item, the code would look like this.

if (SPContext.Current.ListItem.ModerationInformation != null)
{
    SPContext.Current.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
    SPContext.Current.ListItem.ModerationInformation.Comment = String.Format("Published on {0} by {1}",  
                                                   DateTime.Now.ToString(), SPContext.Current.Web.CurrentUser.Email);
    SPContext.Current.ListItem.Update();
    
    //Added to refresh the page content and hide the publishing controls 
    Response.Redirect(SPContext.Current.ListItem.Url);
}
 
To revert a saved list item to it's previously published version, the code would look like below:

if (SPContext.Current.ListItem.ModerationInformation != null && SPContext.Current.ListItem.Level != SPFileLevel.Published)
{
    //Getting the last published version of the current item
    var lastPublishedVersionInfo = SPContext.Current.ListItem.Versions.Cast<SPListItemVersion>()
                                                                            .Where(v => v.Level == SPFileLevel.Published)
                                                                            .OrderByDescending(l => l.Created).Take(1);
    SPContext.Current.ListItem.Versions.RestoreByID(lastPublishedVersionInfo.First().VersionId);
 
    SPContext.Current.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
    SPContext.Current.ListItem.ModerationInformation.Comment = String.Format("Reverted on {0} by {1}", 
                                                               DateTime.Now.ToString(), SPContext.Current.Web.CurrentUser.Email);
    SPContext.Current.ListItem.Update();
 
    Response.Redirect(BlogManager.GetBlogPostUrl(SPContext.Current.Web, SPContext.Current.List.ID, SPContext.Current.ListItem.ID));
}

Hope this helps some one or even myself somewhere in the future.

Thanks
Senthil S

Wednesday, January 4, 2012

Using master page and layouts page in custom list view

I was struggling for couple of days now on how to make use of any master page in a layouts page which is used for a view of a custom list created using custom list schema.xml.

How to create a custom list definition is explained here (http://msdn.microsoft.com/en-us/library/ff728096.aspx)

I was able to create a custom list definition and added a custom list view page also in the schema.xml file itself. Here is how to add a custom list view using a layouts page (application page)

After adding a custom list schema, find the node "<Views>" and add a new "<View>" tag. You can just copy and paste the already existing "<View>" tag which has the BaseViewID="1" (this is the one which constitutes for the web page view. It is important that you should be copying the whole "<View>" node with all the internal elements like below.



To make use of the layouts page for the view, just change the "SetupPath" parameter to point to the corresponding layouts page without the "/" in front of the "layouts" folder (i.e., should be starting as "layouts/setup/settings.aspx") and change "Url" parameter to the name you want to see in the URL.

When I changed the "MasterPageFile" parameter to an application master page like below,

<%@ Page Language="C#" AutoEventWireup="true" 
                           MasterPageFile="~/_layouts/AppMasterPages/Simple.master" %>
 
the custom view gave me the below error

"The referenced file '/_layouts/AppMasterPages/Simple.master' is not allowed on this page"

If the "MasterPageFile" is change to "~masterurl/default.master", everything works well. If you are using a custom master page for the site you may make use of that by setting the "MasterPageFile" to "~masterurl/custom.master".

Happy coding.
Senthil S

Tuesday, October 18, 2011

Get SPContext.Current.ListItem in SharePoint layouts page

I was developing a layouts page to view a list item for anonymous users with custom styles. So I was doing a little research on how to get the reference to the current list item (SPContext.Current.ListItem) so that the field values can be easily bound using SharePoint web controls itself.

It turns out to be, if a layouts page is being referenced relative to a proper web, and the List id and Item id are passed in as parameters, then I can get the SPContext.Current.ListItem referencing to the particular item being viewed and I can make use of the SharePoint Web Controls to display the field values.

http://localweb/{web url}/_layouts/ViewItem.aspx?List={list guid}&ID={itemid}

Example: http://localweb/home/_layouts/ViewItem.aspx?List={E2E0527C-EFB1-42D4-8FFE-20A6AC364845}&ID=1


In the ViewItem.aspx, I can make use of SharePoint web controls in the layouts page to display the field values as:

<SharePoint:FieldValue FieldName="Title" runat="server" />
 
Hope this helps some one else.

Thanks
Happy Coding
Senthil S