Wednesday, July 24, 2013

Sharepoint 2010 List filtering by date range

 

1. Add new item for Task list

2. Go to Site Actions >> Edit Page

3. Add web part Date Filter

4. Edit Web part

5. In the options pane that appears, change the Filter name to 'Start Date', and change the default value to 'offset from today 7 days: Before today'. Then click ok.

6. Underneath that, add another date filter and change its filter name to 'End date', and change the default value to 'offset from today: 0 days: After today'. Then click ok.

7. Now save the page in your browser, then open the page up in SharePoint designer (SPD) - in design view.

8. Edit page in Advanced Mode

9. Once opened in SPD, click on the list web part to select it, then in the ribbon click on the list view tools > options > parameters icon.

10. Add two new parameters, one called 'startdate'. Leave the parameter sources and default values as default.

11. Add two new parameters,  called 'Enddate’. Leave the parameter sources and default values as default.

12. Click OK

13. Click on the start date filter and click add connection

14. In the dialog that pops up change the dropdown value to 'Send filter values to', click next.

15. Ensure that 'connect to a web part on this page' is selected in the next dialog and click next.

16. Change the target action in the next dialog to 'Get parameters from' and click next.

17. In the next dialog, select 'Start Date' from the first box, and 'Startdate' from the second box and click next. Then click finish.

18. Click Finish

19. Click on the End date filter and click add connection

20. In the dialog that pops up change the dropdown value to 'Send filter values to', click next.

21. Ensure that 'connect to a web part on this page' is selected in the next dialog and click next.

22. Change the target action in the next dialog to 'Get parameters from' and click next.

23. In the next dialog, select 'End Date' from the first box, and 'Enddate' from the second box and click next. Then click finish.

24. Click Finish

25. Now we have our date filters feeding the list with its values but we still have to set up the filter on the list. Click the list web part to select it, then in the ribbon click on the list view tools > options > filters icon.

26. The filter criteria box wills popup. First add a rule 'Start Date > Greater than or equal to > [Startdate]'.

27. Click to “Click here to …”

28. Then add another clause 'Start Date > Less than or equal to > [Enddate]'. Click ok.

29. Now save the page and open it back up in your browser. It will, by default, show all tasks Start Date in the last seven days thanks to the default setting of the start date filter. Changing these filters will filter the list according to the date ranges set.

30. You can write your caml query in sharepoint designer


Monday, July 15, 2013

To fix Note Board missing SharePoint 2010

To fix this You have to start the user profile service on Your SharePoint server and connect it to the web application where You added the web part. You can do this by going to Central administration -> Manage web applications -> select the proper web application -> click Service connections in the Ribbon -> Tick the checkbox next to the User Profile Application -> click OK.

If everything went good, You should now see a comment box below the Note Board header:

Using jQuery with Sharepoint 2010

 

1. Download jQuery

Download latest verion of jQuery here http://docs.jquery.com/Downloading_jQuery

2. Save it in an Accessible Location in Sharepoint

Using Sharepoint Designer I added a Folder called “Scripts” inside the “Style Library” of the root level of the site. I then added the latest jQuery File into that folder.

3. Place a reference on that jQuery from your code

The best place for this will be your master page so that it can be used anywhere in your site, so open the master page (by default SharePoint 2010 uses v4.master) you are using in Advanced mode then add your javascript reference like such.

Collapse | Copy Code

<script src="/Style%20Library/Scripts/jquery-1.6.2.js" type="text/javascript"></script>

you place that inside the <head> tag


Save that master page and you are ready to go

4. Test if its working

If you are using FireFox open up firebug or if you are using IE you can press F12. Go to the script tab to check whether your javascript reference is there, then on the console window try wether that reference works by typing something like this.

Collapse | Copy Code

$("#MSO_ContentTable").text("Welcome to Sharepoint jQuery")

you should get a result something like this

Working With 2/3 level Cascading Dropdown In Sharepoint List

 

Sometimes we face such limitations or issues, requirements where we need to do some custom coding in sharepoint. obviously this hampers performance and consumes time in developing forms and testing it.
Simply, i wanted to create a list where i need to implement cascading Dropdowns. most of us are familiar with such scenarios like Country, State, District dropdown are filled up one by one depending on selection of previous dropdownlist. For such simple requirement we may need to invest time in custom code and testing.
Below image can give a better idea about given scenario.

Here i wanted to create a list of category, sub-categories and Store. my store list includes look up for category and sub-category which i need to fill-up on the basis of selection of fist dropdown value. lets see how it works. i will try to guide through images as we all know that we hate reading and require faster output.

List Structure:

1. Created a list Category.(List Name:Category)

Image:1

2.Created Sub-Category List (List Name:SubCategory)

Image:2

In this list, I have look-up column 'RequestCategory' for 'Title' in 'Category' list.

3.Created a list where we expect to select cascading dropdown while adding or editing a item into list(List Name:Store).

Image:3

In this case if you observe add, edit form. it will bring all items from 'SubCategory' (2nd list) and not only in which we are interested in by selecting    Item from 'Category' (1st List).

Solution:

To resolve this problem we need following (.js) files.

1.jquery click here for latest version

2.jquery.SPServices click here for latest version

add content editor webpart where you want to make cascading dropdown work.

put below script into content editor webpart:

<script language="javascript" src="/sites/Home/Requisition/Shared%20Documents/jquery-1.9.0.js" type="text/javascript"></script>

<script language="javascript" src="/sites/Home/Requisition/Shared%20Documents/jquery.SPServices-0.7.2.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">

      ExecuteOrDelayUntilScriptLoaded(loadCascadingDDL, "sp.js");

function loadCascadingDDL() 

                {

                          $().SPServices.SPCascadeDropdowns({

                                       relationshipList: "SubCategory",

                                       relationshipListParentColumn: "RequestCategory",

                                       relationshipListChildColumn: "Title",

                                       parentColumn: "ItemCategory",

                                       childColumn: "ItemSubCategory",

                             });

                }

</script>

Here in above example parameters are below:

relationshipList: is the list name of 2nd image.(SubCategory list)

relationshipListParentColumn:Parent column from 2nd image(parent look-up column in SubCategory List)

relationshipListChildColumn:The child field name which you want to display on second dropdown(from SubCategory List/ 2nd image)

parentColumn:The field name which you want to treat as a parent column(from list where you want to input data/3rd image)

childColumn:The field name which you want to treat as a child column(from list where you want to input data/3rd image)

all this works for add/Edit item as well and we don't require any custom coding.

Output:

Note: it can fail or will not respond, if you do have space,-,(,) characters in your field name.

Going ahead question may raise in your mind that is it possible to create  three level cascading dropdowns? i'll say, why not?

In this case we will repeat same function for next cascading dropdwon.

Three level cascading dropdownlist:

I created a new list 'Request' where i need to refer all above lookups with this list. below is screeenshot for list.

and my output should be:


And my code would be:

<script language="javascript" src="/sites/Home/Requisition/Shared%20Documents/jquery-1.9.0.js" type="text/javascript"></script>

<script language="javascript" src="/sites/Home/Requisition/Shared%20Documents/jquery.SPServices-0.7.2.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">

      ExecuteOrDelayUntilScriptLoaded(loadCascadingDDL, "sp.js");

function loadCascadingDDL() 

                {

                          $().SPServices.SPCascadeDropdowns({

                                       relationshipList: "SubCategory",

                                       relationshipListParentColumn: "RequestCategory",

                                       relationshipListChildColumn: "Title",

                                       parentColumn: "ItemCategory",

                                       childColumn: "ItemSubCategory",

                             });

                          $().SPServices.SPCascadeDropdowns({

                                       relationshipList: "Store",

                                       relationshipListParentColumn: "ItemSubCategory",

                                       relationshipListChildColumn: "Title",

                                       parentColumn: "ItemSubCategory",

                                       childColumn: "Request",

                             });

                }

</script>

Thursday, June 27, 2013

Getting a user's last login date -- Before it's updated by the ASP.NET framework!

Introduction

A quick and easy way to get the user's last login date before the ASP.NET framework updates it during the login process.

How it works

The idea is to catch the Authenticate message before the framework has a chance to do its thing. This will allow us to examine the user's properties before they are actually logged in. Therefore, we are getting the last login date before we authenticate them.

Once, we have a valid user, we store the date into a session variable for later use.

Setting up an example

  • Create a new ASP.NET 2.0 project that uses the Membership Provider. There are lots of samples of this already.
  • Add a login control to your default.aspx page. See the "Login Control" code section below.
  • Verify that this works with your Membership Provider.
  • Add the OnAuthenticate handler to the Login1 control. See "OnAuthenticate Handler" below.
  • In your default.cs file, add the handler function for OnAuthenticate. See the "Authenticate Function" below.
Login control

Add this inside your Default.aspx page's <body> tag:

Collapse | Copy Code

<asp:Login ID="Login1" runat="server" VisibleWhenLoggedIn="False">
</asp:Login>

OnAuthenticate handler

After adding the OnAuthenticate handler, your login control should look like:

Collapse | Copy Code

<asp:Login ID="Login1" runat="server" 
OnAuthenticate="Login1_Authenticate" VisibleWhenLoggedIn="False">
</asp:Login>

Authenticate function

Add the following code inside the partial class in default.cs:

Collapse | Copy Code

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Login login = (Login)sender;
// Get the user attempting to login

MembershipUser user = Membership.GetUser(login.UserName);

if ( user != null )
{
// Get the date the user last logged in before
// the user is validated and the login gets updated
// to the current login date

DateTime dtLastLogin = user.LastLoginDate;

// Attempt to validate user -- if success then set the authenticated
// flag so that the framework knows to allow the user access
if (Membership.ValidateUser(login.UserName, login.Password))
{
Session["LastLoginDate"] = dtLastLogin;
e.Authenticated = true;
}
}
}

Displaying a SharePoint Modal Dialog window from SPGridView

 

I recently built a web part that required a list of announcements with summaries to be presented to a group of users.  Additionally, there was detailed content that was to be displayed in a Pop-up type window.  I used a gridview to display the list, which was pulled in from a web service and passing in a datatable.  That part was rather easy, but the data for the details was stored in a column that contained HTML formed text.  My first thought was to pass that to a javascript function that would call the method for displaying a SharePoint dialog: SP.UI.ModalDialog.showModalDialog();

When I added the HTML as a parameter to the javascript function, the result of doing that was rather odd.  The HTML text was displayed in the web part itself, which was not going to work.  I tried a few workarounds like encoding the HTML text and such, but I did not get good results with that either.  Instead, I knew that I had the ID for the items that I was displaying in the list.  I could pass in the ID to the dialog page and retrieve the HTML text at that point.  Unfortunately, it required two round trips to the web service (and subsequently the SQL Server).

Another issue when displaying the dialog was that the page that I was displaying contained all the elements of the master page that typical pages display (header, footer, etc).  This made the dialog too busy for the simple task it was asked to do.  To fix this, I added the MasterPageFile attribute to the Page directive which overridesDynamicMasterPageFile attribute.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AnnouncementPage.aspx.cs" Inherits="MyPages.WebParts.Layouts.AnnouncementPage" DynamicMasterPageFile="~masterurl/custom.master" MasterPageFile="~/_layouts/applicationv4.master" %>

In the end, a uncluttered summary list and detailed model dialog:

image

image

Code that made it happen:

<script type="text/javascript">
function OpenDialog(aid) {
var options = SP.UI.$create_DialogOptions();
options.url = "/_layouts/MyPages/AnnouncementPage.aspx?announcementId=" + aid;
options.width = 560;
options.height = 480;
options.dialogReturnValueCallback = Function.createDelegate(null, OnCloseDialog);
SP.UI.ModalDialog.showModalDialog(options);
}
</script>

<div class="spgridannounce">
<SharePoint:SPGridView ID="spGridViewAnnouncements" runat="server" AutoGenerateColumns="false"
ShowHeader="false">
<HeaderStyle BackColor="Transparent" ForeColor="#08630C" />
<AlternatingRowStyle BackColor="Transparent" />
<RowStyle BackColor="Transparent" ForeColor="#BF5629" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Announcement</HeaderTemplate>
<HeaderStyle />
<ItemTemplate>
<a href='javascript:OpenDialog(<%# Eval("announcement_id")%>)'>
<%# Eval("subject") %>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</SharePoint:SPGridView>
</div>