Close Modal Dialog in SharePoint 2010

by josh 16. December 2010 04:03

this came in handy for closing custom item edit forms from codebehind:

 

  //Close modal dialog
Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
Response.Flush();
Response.End();

used at the end of button submit handler.

SharePoint 2010 Custom Ribbon Button–Copy Task

by josh 19. November 2010 07:19

The SharePoint 2010 Ribbon is highly customizable. This paper will demonstrate how to add a button to the View Item form that will create a copy of the current item in the list. This example will use the Tasks list, but the steps are generally applicable for any list type. The general steps are as follows:

 

1. Create a new Visual Studio SharePoint 2010 project of the type Empty SharePoint Project

2. Enter the web site for testing and choose Deploy as a sandboxed solution. (Note: If deploying along with workflows or other customizations that required GAC-installed DLLs or files on the file system you will want to use a Farm Solution)

3. In Solution Explorer, right click the project name and click Add -> New Item.

4. Choose Empty Element from the item types and give it a name, such as CustomActions

5. At this point you will have an empty elements file

1_thumb[1]_thumb[3][3]


 

Add the following XML between the Elements tags:

 

<CustomAction   Id="Contoso.CustomActions.CopyTaskAction"
Location="CommandUI.Ribbon"
RegistrationId="107"
RegistrationType="List"
Sequence="1">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.Controls._children">
<Button Id="Contoso.Ribbon.CopyTask"
Command="Contoso.Ribbon.CopyTask"
Sequence="5"
Image16by16="/_layouts/images/NoteBoard_16x16.png"
Image32by32="/_layouts/images/NoteBoard_32x32.png"
Description="Uses the notification area to display a message."
LabelText="Copy Task"
TemplateAlias="o1"/>
</CommandUIDefinition>
</CommandUIDefinitions>
</CommandUIExtension>
</CustomAction>


<CustomAction>

(http://msdn.microsoft.com/en-us/library/ms465980.aspx)

The CustomAction tag is used for deploying custom actions to the SharePoint UI. This includes many types of customizations, such as the Site Actions menu and the item context menus, as well as the ribbon.

The Location attribute specifies where the custom action will be created. For the Ribbon, the location should be CommandUI.Ribbon.

The RegistrationId and RegistrationType attributes specify what type of objects to attach the customization to as well as the specific IDs of the specified type. In this case, we want the customization to be attached to a list of type 107 (107 is the list template ID for the Tasks list).

Sequence is used throughout SharePoint declarative programming and refers to the order in which an element appears in the UI. Lower sequences appear first. SharePoint typically uses multiples of 10, but you will probably have to check the definitions yourself if you’re trying to place a customization in a very specific location, like between two other SharePoint elements.

<CommandUIDefinition>

(http://msdn.microsoft.com/en-us/library/ff458373.aspx)

This is used for adding specific elements to the SharePoint UI.

The Location attribute of the CommandUIDefinition specifies exactly where in the Ribbon the customization should be located. In this case we want the button to appear in the Manage group of the list item Display form. Specifying Controls._children tells SharePoint to add the customization to the controls collection of the specified group.

<Button>

(http://msdn.microsoft.com/en-us/library/ff458366.aspx)

As might be expected, the Button tag represents a button in the Ribbon.

The Command attribute specifies the command to be executed when the button is clicked. This will be used in the CommandUIHandler to execute some JavaScript code.

The Image16by16 and Image32by32 attributes allow specify the large and small icons to be used with the button. The 16 and 32 represent pixel sizes for the respective images.

The LabelText attribute is the text that will be displayed on the button in the Ribbon.

The TemplateAlias attribute specifies a display template for the button. These are defined by SharePoint and can be found in the SharePoint root directory at TEMPLATE\GLOBAL\XML\CMDUI.XML. In this case, setting the attribute to “o1” will display a large icon similar to the “Edit Item” button, and “o2” will display a small icon similar to the “Delete Item” button.

 

6. Deploy the solution from Visual Studio. At this point you will see the button in the View form but it will be disabled because there are no handlers defined for it yet.

2_thumb_thumb_thumb[3]


 

Now copy the following XML between the closing CommandUIDefinitions and the closing CommandUIExtension tags:

 

<CommandUIHandlers>
<CommandUIHandler
Command="Contoso.Ribbon.CopyTask"
CommandAction
="javascript:
if(confirm('Copy Task, are you sure?')){
var ctx;
var taskList;
var web;
var taskItem;

//Get client context
ctx = new SP.ClientContext.get_current();
//Get reference to current web
web = ctx.get_web();
//Load web into context
ctx.load(web);

//Get reference to current list
taskList = web.get_lists().getById('{ListId}');
//Load list into context
ctx.load(taskList);

//Get reference to current list item
taskItem = taskList.getItemById('{ItemId}');
//Load listitem into context, including specified columns.
ctx.load(taskItem);

//Perform batched queries on server
ctx.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}




function onQuerySucceeded(sender, args) {

//Create ListItemCreationInformation for use when creating new task item
var taskItemInfo = new SP.ListItemCreationInformation();

//Create new request item
newTask = taskList.addItem(taskItemInfo);

//Set properties on new request from current request
newTask.set_item('Title','COPY OF TASK {ItemId}');
newTask.set_item('Body',taskItem.get_item('Body'));
newTask.set_item('AssignedTo',taskItem.get_item('AssignedTo'));
newTask.set_item('DueDate',taskItem.get_item('DueDate'));
newTask.set_item('Priority',taskItem.get_item('Priority'));
newTask.set_item('StartDate',taskItem.get_item('StartDate'));
newTask.set_item('TaskGroup',taskItem.get_item('TaskGroup'));

//Update database with changes
newTask.update();

//Load new request into context
ctx.load(newTask);

//Perform batched operations on server
ctx.executeQueryAsync(requestAddSucceeded, requestAddFailed);

}



function requestAddSucceeded(sender, args) {
//Notify user that copy succeeded
SP.UI.Notify.addNotification('Task copied successfully');
}

function requestAddFailed(sender, args)
{
//Notify user that copy failed
SP.UI.Notify.addNotification('Task failed to copy!');
}

function onQueryFailed(sender, args)
{
//Notify user that an error occurred
SP.UI.Notify.addNotification('Error copying task!');
}

"
/>
</CommandUIHandlers>


<CommandUIHandler>

(http://msdn.microsoft.com/en-us/library/ff458385.aspx)

This tag defines a handler for a custom action command. In this case we want to define a handler for the Contoso.Ribbon.CopyTask command, which we specify in the Command attribute.

The CommandAction attribute is where we define the handler for the command. Here we add the javascript to actually perform the copy using the SharePoint ECMAscript Client Object Model. I won’t go into detail here about programming against the Client OM, but there are many good articles, blog posts, etc. out there. Some good ones I’ve found are *here*…

In a nutshell, the javascript code gets a reference to the task using the {ListId} and {ItemId} tokens, which are replaced at runtime with the IDs of the list and item from the current context (e.g. the item being displayed in the View form). Then a new task is created in the same list and the properties are populated from the current task. As far as I can tell, this is the best way to perform a copy on a list item. There is no CopyTo method or anything like that, at least that I could find.

 

7. With the CommandUIHandlers code added, redeploy the solution. You will now be able to click the Copy Task button. 

3_thumb_thumb_thumb[3]


and when you click the button you will be prompted to confirm your desire to copy the task… 


4_thumb_thumb_thumb[3]


and when you click OK you will be notified, using the new notification framework, that the task copied successfully.


5_thumb_thumb_thumb[3]

 

8. Refreshing the list view, you will see the original task and the new copy.


6_thumb_thumb_thumb[3]

 

Here’s a few helpful blog posts related to Ribbon Customizations and the ECMAscript Client OM:

  • Chris O’Brien’s series on Ribbon Customizations
  • Channel 9 Developer Training videos on the Client OM

Deploying Custom SharePoint List Forms with VS 2010

by josh 20. September 2010 08:59

Most of the time Visual Studio 2010 just handles the deployment of files for you and you don’t have to think about it. Today I’ve been trying to figure out how to get a file deployed that I copied into the project – in my case a custom newform.aspx for a list definition. I copy this file into the list definition node in my project and expect that Visual Studio will pick it up and deploy it for me with the elements.xml and schema.xml.

 

image

But to my consternation the file is completely ignored. Argh… So after much frustration and manually editing of feature manifest files I found the answer right in front of me. Files in a SharePoint 2010 project have properties associated with them, and one of those properties is DeploymentType which is set to NoDeployment by default . So instead of trying to manually edit the feature manifest to include your ElementFile entry (which doesn’t work, by the way), you just change the DeploymentType to ElementFile. Dang that was so easy.

 

image

You can also specify exactly where the file should be deployed within the root, which is the feature folder in the case of a feature. The property for this is just above DeploymentType and is called DeploymentLocation.

I love VS 2010 more every day!!

What happens when you search for “mac” from Microsoft BPOS KB…

by josh 9. August 2010 03:29

image

Best Practices Conference 2010

by josh 23. June 2010 05:59

bpconf_'banner304x168

I’ll be there. The conference in San Diego a couple of years ago was excellent. Wish they would have it there again…

Canoeing the Paint Rock River

by josh 16. June 2010 02:38

Kimberly and I took Isaiah on an overnight canoe trip down the Paint Rock River east of Huntsville. It was his first time in a canoe, pretty hilarious.

Getting geared up…

IMG_1367

He’s pretty excited…

IMG_1370

Stopped for the night…

IMG_1373

Our camp site.

IMG_1377

Taking a dip at the take-out. I don’t know who the fat guy is in the picture.

IMG_1383

 

IMG_1382

Wish we’d actually gotten some pictures of him in the canoe, but I think we were so unsure how he would handle it we were afraid to keep the camera out in case he sent us for a swim. Next time!

Screw you, comment spammers!

by josh 13. June 2010 13:14

I just upgraded to BlogEngine.Net 1.6.1, which enables reCAPTCHA as an extension. Very Nice. I've been getting 10 or more spam comments a day and I'm really sick of them. Spammers will be in the very, very inner circle of Hell with telemarketers and pompous, incompetent IRS agents.

FileNotFound Exception when creating SPSite or getting reference to SPWebApplication (SharePoint 2010)

by josh 10. June 2010 04:10

Ok, this is the second time I’ve had this problem, so I’m going to blog about it so maybe I’ll remember next time. If you’re using VS 2010 and developing outside of IIS (console application in my case), you need to make sure your project is not targeting the x86 Platform. Go to your Project Properties, click the Build tab, and make sure the Platform target dropdown is set to x64 (or Any Platform). Another less than helpful error…

Create Root Web from Template in a SharePoint 2010 Site Collection

by josh 21. May 2010 05:32

In SharePoint 2007, you had to use stsadm.exe to install site templates to be used for creating the root site in a site collection. In SharePoint 2010, you still have the command-line option available with the PowerShell cmdlet Install-SPWebTemplate, but now it can also be done through the UI when creating a site collection.

 

To use a custom template to create a site collection, go to Central Administration –> Application Management –> Create Site Collections.

Fill out the information for your new site collection, choosing the Custom tab and “< Select template later… >”. Click OK to create the site collection.

 

image

 

When you get the Top-Level Site Successfully Created page, click the link to the new site collection. You’ll see the Template Selection page shown below.

 

image

 

Click the link to the solution gallery, where you can upload your desired site template. Don’t forget to Activate the template as well so it will be available for use.

image

Notice where you are – the solutions gallery of your newly created site. So you can upload templates to the solutions gallery of the site before you’ve chose a template for the site. Upload the solution and then navigate to the address of your site again, where you’ll be presented with the Template Selection page.

Now you can choose the Custom tab, and you will see your custom site template available to choose.

image

smtp4dev - Awesome development tool

by josh 4. May 2010 06:58

At SharePoint Saturday Huntsville I showed a tool during my workflow presentation that runs in your tray and monitors Port 25 for SMTP traffic. When a message gets sent to Port 25 (say, from the SharePoint timer job), this tool grabs it and shows it in a nice little grid, and also displays a popup notification if it’s minimized to the tray. Check it out:

 

image

 

image

 

You can grab this tool at http://smtp4dev.codeplex.com. Just run the EXE and it will start monitoring. Minimize and it will go to the system tray and alert you when a message comes through. Well done, rob@rnwood.co.uk!