Thursday, November 21, 2013

Open SharePoint 2013 List Items Directly into Edit View

I had written another post on this blog about opening list items directly into Edit View, which was based on SharePoint 2010.  Since then I have received several requests on how to do this in SharePoint 2013.  So here we go.

The original post is here: Open SharePoint 2010 List Items Directly into Edit View

In the original post I had given two options to perform a direct edit on list items:
  • The Brute Force - Using jQuery to modify the HTML on the page
  • Elegant (Custom List View) - This used a custom list view where we edited the link for the item
Now, the issue with SharePoint 2013 and SharePoint Designer 2013 is that the Design View is now gone when working with lists!  So, based on my earlier post, the Elegant method no longer applies.  As you can see in the screen shot below, there is no Design option anymore.


Luckily the first option, Brute Force, will still work!  And even better, there is the new Script Editor Web Part in SharePoint 2013, which makes this a more elegant solution that in SharePoint 2010.

First here is the script:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(function() {
  // Change all display form links to edit form links
  $
('.ms-vb a[href*="listform.aspx"]').each(function(){
     
var link = $(this).attr('href');
      link
= link.replace("PageType=4", "PageType=6");
      $
(this).attr('href', link);
  });
});
</script>

This script is pretty straight forward, it looks for all links within a class of .ms-vb that have listform.aspx in their value.  If you inspect the links on your form you'll see they are wrapped in a DIV with this class applied.  After it finds all of the links it pulls the href value out and performs a replace changing the PageType to 6 (which is the type for Edit).  It then sets the link back to this new updated value.

To apply this to your list, simply navigate to your list and add a Script Editor Web Part.  In this example I have a simple list called List 1:

If we inspect the link of the individual list items, we'll see the link goes to .../listform.aspx&PageType=4....


Next, place the page into Edit mode and click the "Add a Web Part" link.  From the menu, choose the "Media and Content" category, and under "Parts" choose "Script Editor":


Click the "Edit Snippet" link on the Script Editor Web Part:



Paste the script from above into the editor and click the "Insert" button.  One thing to note here, if you already have jQuery on your page like in the MasterPage, then don't include the first line.  This simply loads jQuery from a CDN, so if you already have it, you don't need that line.


Once the snippet is inserted, click "Stop Editing" on the page to save the changes.  Now if you inspect the links of the page, you should see that the "PageType" query string parameter has been updated to '6' in the listform.aspx link:


Now clicking the list item will open the form in Edit mode:

If you have your list setup to launch forms in dialogs, it still works:


And there you go!  Again, not as elegant as being able to work directly with the list as you could in SharePoint 2010.  However, having a dedicated web part for scripts makes it an easy solution to implement.

Enjoy!

4 comments:

  1. you must be using the link to item with edit menu for it to appropriately copy your contenttypeID to open the page. For example, if you are using a linkToItem field, you need to also use link = link.replace("ContentTypeID=", "ContentTypeID=0x0*whatever your content type guid is");
    $(this).attr('href', link);

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. Thanks, works great except on grouped lists. No biggie.

    ReplyDelete