Add new Item prompt in SharePoint List

In a SharePoint list after clicking on the “Add new item” button, users will be taken to a form, fill out information and click save. At this point the form closes and the user can see the new item in their list. But what about when users want to add several items at once without clicking “Add new item” each time?

Luckily, there is the PreSaveAction() function that can be used to perform certain actions before the item is saved to the list:

preSaveAction

Thanks to a little hunting and this stack overflow post, I was able to learn that PreSaveAction() allows the user to override the default save button click behavior. In my example, I wanted to prompt the user to create another item or close the form. This can be accomplished with a simple javascript alert, as seen above in my defined PreSaveAction() function. What I’m doing is getting the current form url and saving it to a variable called current URL.

First, CurrentURL will look something like this: http:// [SharePoint ]/ [site] / [list] /newForm.aspx.

Secondly, I am saving the source parameter (where I want to redirect to — this form to add another item), in a variable called redirect. redirect will look something like this: ?Source=http://[SharePoint]/[site]/[list]/newForm.aspx. Keep in mind that appending the ?Source parameter to a SharePoint URL will control where the user is redirect after saving an item. In my case I can redirect them back to the newForm after saving an item.

Third, on the save button click, I call my PreSaveAction() function, which prompts the user to create another item, if they say no, the item is saved and the window is closed. If they say yes, window.location.search is set to the redirect variable. window.location.search returns the querystring which is exactly the part I want to set. This will then save the item and then redirect the user to the newForm where they can submit another item.

Format Date

One of the things I find myself looking up frequently is Date Values for SharePoint. When creating custom display forms, you typically end up with date and time values that look something like this: 2013-07-24T21:00:00Z. This isn’t really a useful value (to an end user). To change it to something more readable open designer and the disp form:

Find the date field:

My Start Time field looks like this in the form:
date_field
The corresponding code in designer:
date_field_code
The xsl:value of tag contains the information to modify. The select attribute is equal to the EventDate value, which is the Start Time. To make a more readable value, it can be changed to:
date_field_code_changed
The ddwrt namespace must be included to use a function like FormatDate. FormatDate, like the name implies, will change the formatting shown above to something useful or readable to an end user. Explanation of the parameters taken from the msdn page:
“The parameter szDate is converted to a DateTime. Based on the formatFlag parameter, which can have a value 0–15, a new DateTime string is constructed in the given locale lcid. Table 5 shows the results returned from this function.”
The above code will result in the below; a nicely formatted date for end users:
date_field_changed
Check the msdn article for a list of formats.

Multiple check boxes default values

I recently had a requirement to have a choice column with check boxes and have all options selected by default. After a little hunting I came across the syntax which is rather easy:

=”;#[Choice1];#[Choice2];#[Choice3];#[Choice4];#”

In the default value box, select Calculated Value and enter the above.

This leads to check boxes being selected on item created.

Better ULS Logging in PowerShell

Stumbled upon this post when trying to use the ULS logger to see an error:

http://www.habaneroconsulting.com/insights/An-Even-Better-Way-to-Get-the-Real-SharePoint-Error#.UvVtBPldV8E

See the blog above for full details but here is a simplified way to get an error:

1. Get correlation id from window

2. in PowerShell (run the following commands):

Merge-SPLogFile -Path “.\error[x].log” -Correlation “85ea729c-071c-d0b1-d6c7-065c6284a50f”

Dir *.log

I name my log errorx where x is a number and delete them after I I’m done.

Saves to C:\Users\ [your user name]

3. Open ULS Viewer > Open File and select error[x].log

Way easier than hunting through ULS logs.

Content Query ‘No results” message

By default Content Query Web Parts (CQWP) don’t display any text on a page when no results are returned. The message is only displayed in “edit mode” when a content manager or developer is editing the page. This can be frustrating because you might want to display a message that says that nothing has been returned. In my case we wanted to query a calendar for upcoming events that meet some conditions and a message if there wasn’t any. The solution is simple:

1. Open Designer and your root site collection (http:// [site collection name])

2. Select All Files > Style Library > XSL Style Sheets

3. Open ContentQueryMain.xsl  (good idea to make a copy called custom)

4. Look for the item template OuterTemplate.Empty:

contentQuerymain

Note the xsl:if tag that indicates that the message will only be shown when in edit mode. Add a custom message above.

Running previous versions of workflows

Recently, we upgraded to SharePoint 2013 from SharePoint 2010 and one of the strangest things that happened in the transition was several workflows were running incorrectly. Simple solution: recreate them.

contribute_feedbackThankfully none were too complex and were recreated quickly. Our most important workflow is arguably a feedback workflow that allows users to click on a “Contribute Feedback” link at the bottom of every page. There are two workflows attached to this list: Feedback and FeedbackClosed. Feedback runs on Item Created and captures the user who created the item, the page they are on (which the feedback applies to), the message in the feedback, the category (typo, missing information,  incorrect information, etc.) and the urgency (Low, High). This workflow sends an email to our dev team with links to the page the feedback applies to and the feedback record.

Feedback also contains a status field and the dev team can change this to completed which triggers the FeedbackClosed workflow and sends a response to everyone on the dev team and the user who created the item with a message of the resolution.

The issue was during recreating the Feedback workflow I misspelled Feedbacik, so I ran the workflow and then saw this in the email:  misspelling. So I opened the workflow in SharePoint Designer and corrected it. I saved and published and created a test workflow to see the same problem: misspelling.  After a few hours of trying to figure this I realized the mistake I made. I navigated to the Feedback list and selected Workflow settings to see about twenty previous versions on this page as well as my Feedback current version that I wanted to run. I selected “Remove” for all previous version and “Allow” for the current version. This ran the correct version.

TL;DR: If previous version of workflows are running navigate to the list > Workflow settings and remove all previous versions.

Display attachments column in Data View

Lately,  for me, it seems we have to come up with creative solutions around SharePoint constraints. Recently, I found a quick way to display the attachments column of list in a Data View:

<xsl:attribute name=”runat”>server</xsl:attribute>
           <xsl:attribute name=”ListId”>{0C830E35-11C8-4846-BDFF-A62A8CE0EF8B}</xsl:attribute>
           <xsl:attribute name=”FieldName”>Attachments</xsl:attribute>
           <xsl:attribute name=”ControlMode”>Display</xsl:attribute>
           <xsl:attribute name=”Visible”>true</xsl:attribute>
           <xsl:attribute name=”ItemId”><xsl:value-of select=”@ID”/></xsl:attribute>
           </xsl:element>
The result is a clean Data View with all attachments for that list item shown. Quick trick to make sure attachments are displayed when you might need them.

SharePoint Calendars

Recently we had an interesting request on our SharePoint intranet: sharing between calendar but with an emphasis on bidirectional aggregation. If an event was updated in a sub site, it need to be reflected in the Master Calendar of the home page. OOTB, I’m not aware that this is possible, so we came up with an intermediary step: send an email to an Content Manager and have them “approve” or save the information to the Master Calendar. The email would include a link to the Master Calendar’s newform.aspx. We managed to accomplish this through a simple workflow and the magically powers of jQuery to auto fill the form for the Content Manager  with the information from the event to be shared in the Master Calendar.

On the sub-site calendars we created a new column for each list called Share With Master, then each calendar has a simple workflow when an item is created with the following logic:

If Share With Master = Yes
{
     Email Content Manager [of the Master Calendar] a link
}

The link sent to the Content Manager contains each of the field values filled out in the querystring. (it might look something like /newform.aspx?Title=test%20title&Location=test%20location)

The link opens newForm.aspx of the Master Calendar and we use jQuery to populate each field of newForm.aspx so the Content Manager just clicks save and “approves” the information. To parse out the above example we would do the following:

    //get parameters
    var title = getURLParameter('Title');
    var location = getURLParameter('Location');

    //remove characters like %20
    var parsedTitle = decodeURI(title);
    var parsedLocation = decodeURI(location);

    //assign to fields
    $("span input[title='Title']").val(parsedTitle);
    $("span input[title='Location']").val(parsedLocation);

This doesn’t give us the bidirectional functionality we were looking for but does allow us to easily share events between calendars. Currently, we’re looking in Bamboo Solutions’ Calendar Plus Web Part which looks like it might meet our requirements.

Entity Framework Database First Walkthrough

This walk-through will cover how set up entity framework with a database first approach:

1. Install Entity Framework 4.1

http://www.microsoft.com/en-us/download/details.aspx?id=8363

2. Begin a new Project

Create a new model:

Right click on project–> add new item
Select data from the menu
Select ADO.Net Entity Data Model
Enter a name
Click add

Follow the prompts and to map to an existing database (ex. use a connection string)

3. Use dbContext code generation

On the design surface of the edmx model:

Right click–> add code generation item
Select code from the menu
Select ADO.Net dbContext generator
Enter a name (helps if this is the same as your model name)
Click add

This will create two new items in your project
[yourname].tt
Which will contain simple POCO classes for each entity in your model
[yourname].tt
Which will generate a drived dbContext to use for getting and changing data

4. Manipulate data

As an example, map to a gridview:

<asp:GridView ID=”GridView1″ AutoGenerateColumns=”false” runat=”server”>
<Columns>
<asp:BoundField DataField=”Category” HeaderText=”Category” />
<asp:BoundField DataField=”CreatedByUserName” HeaderText=”User” />
<asp:BoundField DataField=”DateTime” HeaderText=”DateTime” />
</Columns>
</asp:GridView>

In the code behind:

InsightEntities topic = new InsightEntities();
GridView1.DataSource = topic.Topics.ToList();
GridView1.DataBind();

Here InsightEntites is the name of our contextClass.

This will something like produce the following:

Category User DateTime
test category test user 09/01/2013