Wednesday, July 9, 2014

How to create cascaded drop down using Client Side Object Model in SharePoint 2010 or above.

When we are trying to cascade drop-downs usually we use server side cascading which causes Post back and reload of controls View-state. Which leads to reset of fields to avoid it we used to use update panel. So instead of doing it we can use client side ECMA Script to get these values. It will make it more user friendly.

Here I am considering we have drop-down list bound from a list which works as a Parent List for second drop-down with a look-up column. We will call these methods on the first  drop-down change event :

function getDocumentType(index, sourceColumn, listName) {
        // get the list by it's name from the root site level
        var Query = '<View><Query><Where><Eq><FieldRef Name="' + sourceColumn + '" LookupId="TRUE"/><Value Type="Lookup">' + index + '</Value></Eq></Where></Query></View>';
        // Get the current client context to start. Most things run from the client context.
        clientContext = new SP.ClientContext.get_current();
        var myList = clientContext.get_site().get_rootWeb().get_lists().getByTitle(listName);
        // use a standard syntax CAML query to filter your results and the fields returned if required, by adding the following, or passing parameters to the load command below
        var query = new SP.CamlQuery();
        // You can leave out this line if you just want to return the entire list.
        query.set_viewXml(Query);
        // add your query to the getItems command for your list
        this.collListItems = myList.getItems(query);
        // issue the load command, these can be batched for multiple operations
        clientContext.load(collListItems);
        // execute the actual query against the server, and pass the objects we have created to either a success or failure function
        clientContext.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction), Function.createDelegate(this, this.myFailFunction));
    }


    function mySuccessFunction() {
        var destDropDown= this.document.getElementById('<%=ddChild.ClientID%>');
        destDropDown.options.length = 0;
        var listItemEnumerator = this.collListItems.getEnumerator();
        var count = this.collListItems.get_count();
        destDropDown.options[destDropDown.options.length] = new Option("-Select-", "0");
        if (count != 0) {
            while (listItemEnumerator.moveNext()) {
                var currentItem = listItemEnumerator.get_current();
                destDropDown.options[destDropDown.options.length] = new Option(currentItem.get_item(destColumn).get_lookupValue(), currentItem.get_item(destColumn).get_lookupId());

            }
        }
    }

    function myFailFunction() {
        alert("Error: Failed to fetch items. Please contact your Administrator.");
        // do something to alert the user as to the error here.
    } 

We can make these function generic for multiple level of cascading.

Thursday, February 6, 2014

How can you use PortalSiteMapProvider to query SharePoint List Items. Through Console or Webparts.

Sometimes we need to query some Items from a SharePoint List which data is not changing so frequently. In this case we need to add values of these list Items into cache. For that we need to add it manually to our cache. And also need to add an event receiver in case of any change in data to refresh cache. However SharePoint provides  "PortalSiteMapProvider" class to load this set of queried items in cache and refresh in case of changes. Here is the way to use this, hope it helps :

For a console application as we will not get HttpContext. So we need to create one as highlighted part otherwise you will get PortalWebSiteMapNode object null :

public static void GetConfigListItemsThroughConsole(string strValue1, string strValue2)
        {
            try
            {
                // Get the current SPWeb object. 
                using (SPSite objSPSite = new SPSite("http://servername:portno/sites/sitename"))
                {
                    using (SPWeb curWeb = objSPSite.OpenWeb())
                    {

                        // Create the query. 
                        SPQuery curQry = new SPQuery();
                        curQry.Query = string.Format("<Where><And><Eq><FieldRef Name='Column1' /><Value Type='Text'>{0}</Value></Eq><Eq><FieldRef Name='Column2' /><Value Type='Text'>{1}</Value></Eq></And></Where>", strValue1, strValue2);

                        // Create an instance of PortalSiteMapProvider. 
                        PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;

                        if (HttpContext.Current == null)
                        {
                            HttpRequest request = new HttpRequest("", curWeb.Url, "");
                            HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
                            HttpContext.Current.Items["HttpHandlerSPWeb"] = curWeb;
                        }
                        SPWeb curWeb2 = SPControl.GetContextWeb(HttpContext.Current);

                        PortalWebSiteMapNode pNode = ps.FindSiteMapNode(curWeb2.ServerRelativeUrl) as PortalWebSiteMapNode;
                       
                        // Retrieve the items. 

                        SiteMapNodeCollection pItems = ps.GetCachedListItemsByQuery(pNode, "ListName", curQry, curWeb);

                        // Enumerate through all of the matches. 
                        foreach (PortalListItemSiteMapNode pItem in pItems)
                        {
                            Console.WriteLine(Convert.ToString(pItem["Title"]));
                            Console.ReadLine();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occured :" + ex.Message.ToString());
                Console.ReadLine();
            }

        }
Just call this method from any windows or console application event.

Now, For a SharePoint as we will have HttpContext here :

public static string GetConfigListItemsThroughWP(string strValue1, string strValue2)
       {
           string resultValue = string.Empty;
           try
           {
              
               // Get the current SPWeb object. 
               SPWeb curWeb = SPControl.GetContextWeb(HttpContext.Current); 


                       // Create the query. 
                       SPQuery curQry = new SPQuery();
                       curQry.Query = string.Format("<Where><And><Eq><FieldRef Name='Column1' /><Value Type='Text'>{0}</Value></Eq><Eq><FieldRef Name='Column2' /><Value Type='Text'>{1}</Value></Eq></And></Where>", strValue1, strValue2);

                       // Create an instance of PortalSiteMapProvider. 
                       PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;

                       //PortalSiteMapProvider ps = PortalSiteMapProvider.CurrentNavSiteMapProviderNoEncode;

                      
                       PortalWebSiteMapNode pNode = ps.FindSiteMapNode(curWeb.ServerRelativeUrl) as PortalWebSiteMapNode;
                       //PortalWebSiteMapNode pNode = ps.FindSiteMapNode(curWeb.Url) as PortalWebSiteMapNode;
                       // Retrieve the items. 

                       System.Web.SiteMapNodeCollection pItems = ps.GetCachedListItemsByQuery(pNode, "ListName", curQry, curWeb);

                       // Enumerate through all of the matches. 
                       foreach (PortalListItemSiteMapNode pItem in pItems)
                           {
                              resultValue =  Convert.ToString(pItem["Title"]);
                           
                           }
                       }
                       catch (Exception ex)
                       {
               
                       }
           return resultValue;

       }

Here we do not need to create a HttpContext, all we need to do to call this method from any webpart/SharePoint event. 

Friday, January 10, 2014

Easiest way to get query string parameters value in JavaScript in Sharepoint.

As a developer we need to get QueryString parameter values so many times. For this simple requirement we end up using some JQuery Library only for this purpose or writing lot of code in JavaScript. Here is good news for SharePoint developer. "JSRequest" class is available in SharePoint to get QueryString parameter value without any fuss.

Here is the way to use it :

/*Call EnsureSetup method to initialize JavaScript*/
JSRequest.EnsureSetup();
/*Suppose your site url is <siteurl>?param1=value1&param2=value2*/
var param1Value = JSRequest.QueryString["param1"];
var param2Value = JSRequest.QueryString["param2"];

I am not 100% that it will work in all the version on SharePoint or not. But It will definitely work in SharePoint 2010 / 2013. This approach will save your effort, and time. Also it will reduce the chance of bug injection.


Tuesday, April 2, 2013

How to generate a PDF report from a asp.net Data Table or SharePoint List using Itext C# dll.

I am going to show you that how to generate a PDF from a given data table using Itextsharp.dll and  Itext sharp.pdfa.dll. We can download both of  them from this link. I will create a Webpart which will contain a button on which click we will be able to generate a PDF report for list data on file system.

Here are the steps :

1.) Very first we need to add these two dlls in our GAC.
   
   i)   Itextsharp.dll 
  ii)   Itextsharp.pdfa.dll 

2.)  Now we need to add a webpart. In webpart we need to add only this code :

 protected override void CreateChildControls()
        {
            Button btnExport = new Button();
            btnExport.Text = "Export";
            btnExport.Click += new System.EventHandler(this.btnExport_Click);
            Label objLabel = new Label();
            this.Controls.Add(btnExport);
            this.Controls.Add(objLabel);

        }



        protected void btnExport_Click(object sender, EventArgs e)
        {
           
                    SPWeb objSPWeb = SPContext.Current.Web;
                    SPList objSPList = objSPWeb.Lists["ListName"];
                    DataTable objDT = objSPList.Items.GetDataTable();

                    //Here you can use a Caml Query to do all types of filtering of data and columns.
                    ExportDataToPDFTable(objDT,
"Poc1" , objSPWeb);
                   
        }

        private void ExportDataToPDFTable(DataTable dt, string PDFName, SPWeb objSPWeb)
        {
            Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            try
            {

                string pdfFilePath = @"D:\POCs\" +
PDFName + ".pdf";

                //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
                PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));

                doc.Open();//Open Document to write

                Font font8 = FontFactory.GetFont("ARIAL", 7);
                Font font9 = FontFactory.GetFont("ARIAL", 8);

                //Write some content
                Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document ");

                //DataTable dt = GetDataTable();

                //DataTable dt = dtStructure.Clone();
                //dt.Rows.Add(objDataRow);

              

                if (dt != null)
                {
                    PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                    PdfPCell PdfPCell = null;
                    foreach (DataColumn item in dt.Columns)
                    {
                       
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(item.ColumnName, font9)));
                        PdfTable.AddCell(PdfPCell);
                    }
                   
                    //How add the data from datatable to pdf table
                    for (int rows = 0; rows < dt.Rows.Count; rows++)
                    {
                        for (int column = 0; column < dt.Columns.Count; column++)
                        {
                            PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                            PdfTable.AddCell(PdfPCell);
                        }
                    }

                    PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table

                    doc.Add(paragraph);// add paragraph to the document
                    doc.Add(PdfTable); // add pdf table to the document

                }

            }
            catch (DocumentException docEx)
            {
                //handle pdf document exception if any
            }
            catch (IOException ioEx)
            {
                // handle IO exception
            }
            catch (Exception ex)
            {
                // ahndle other exception if occurs
            }
            finally
            {
                //Close document and writer
                doc.Close();

            }
        }

Through this you will be able to generate PDF with least coding from a DataTable in C#.

Monday, March 18, 2013

Easy way to validate User againsts Active Directory in C#.

If you want to validate a user against an active directory. This is the easiest way provided by .Net framework. By using this we do not need to explicitly create a LDAP web service reference. For validating user we need to include this System.DirectoryServices.AccountManagement.dlll reference in our solution. After adding this dll our code will be very simple,  here it is :

Include this two Namespace (You can resolve it as well) :

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement; 




Here I am taking value to validate a from a text box.

string Domain = string.Empty;
string UserName = string.Empty;
string strUserName = txtAdminName.Text;
 

if (!string.IsNullOrEmpty(strUserName))
 {
 string[] DomainAndUserName = strUserName.Split('\\');

                 if (DomainAndUserName != null && DomainAndUserName.Length > 0)
                       {
                            Domain = DomainAndUserName[0];
                            UserName = DomainAndUserName[1];
                        }
                    }
                    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
                    {
                        UserPrincipal up = UserPrincipal.FindByIdentity(pc, strUserName);
                        bool UserExists = (up != null);
                        if (UserExists)

                        {
                        // Do whatever action you want to perform, when user exists.
                        }
                        else
                       {
                        // Do whatever action you want to perform, when user does not exist.              
                        }
                     }
   }

You can place this code in a  function and then use. Hope it helps...!!

Tuesday, March 5, 2013

How to Query SharePoint List Items Using REST and ECMA script which returns as JSON.

Here is the way you can query data from a SharePoint List by using REST and then return all these items in JSON format. You need to call these two methods with all required values. Here I am going to query a list in which "Title" column contains value 'CR' :

 This method you can call in inn your any event, where you want to access data of a SP list through REST in JSON format :

function getListItems() {

var Url = "http://<servername>:<portno>/sites/<sitename>/_vti_bin/listdata.svc/<ListName>?$filter=substringof('CR',Title)";

//Create a WebRequest object
var request = new Sys.Net.WebRequest();

//Specify the verb
request.set_httpVerb("GET");

//Use the URL we already formulated
request.set_url(Url);

//Set the Accept header to ensure we get a JSON response
request.get_headers()["Accept"] = "application/json";

//Add a callback function that will execute when the request is completed
request.add_completed(onCompletedCallback);

//Run the web requests
request.invoke();
}


This function runs when the web request completes so make sure you will get response.

function onCompletedCallback(response, eventArgs) {
//Parse the JSON reponse into a set of objects by using the JavaScript eval() function

var Items= eval("(" + response.get_responseData() + ")");
alert(
Items);
//Fomulate HTML to display results
var Column1 = "
Column1 : ";
var Title = "Title : ";
alert(
Items.d.results.length);Column1 += 'Title: ' + Items.d.results[0]['Content'];
Title += 'Title: ' +
Items.d.results[0]['Title'];

//var IDs= "IDList : ";
//var Titles = "TitleList : ";

 //for (var i = 0; i < Items.d.results.length; i++) {
//Display some properties
//Titles += 'Title: ' + Items.d.results[i]Title + ";
//IDs += 'ID: ' + Items.d.results[i].Id + ";
//}


//Display the raw JSON response
var
Raw JSON response = 'Raw JSON response:' + response.get_responseData();
alert(
Column1 );
alert(Title);
}


So by using this code you can get response from a list without creating so many objects(e.g. : Web, site, list, Caml Query and many more). These three alert will show Raw JSON response, Column(which can be any) and Title. Which you can customize further for use.

* Highlight 1: Its a contain filter, you can apply any as per your need. There is a very rich set of keywords there to filter.


* Highlight2: You can un comment this code and it will give you collection of items.



 

How to pass a value across Approval Levels and Check First Approver is not same as Second Approver in OOB SharePoint Designer Approval Workflow.

Here in one of my requirement I need to pass a value which is entered in level one of approval task from to second level of approval task form. After a  lot of search I was not able to find anything. This is the way I have done it. I will also demonstrate a way to check first app rover is not same as second. These are the steps that be useful for someone looking for same :

Step 1: We need to take two workflow variable on the completion condition of level 1 approval as shown in the below screen shot :


 Through these condition we can get our required values in workflow variables.(Here ID14743  is any field you want to save in this variable  and Modified By contains name of the first app rover. )


Step 2: Now save these two values in two respective fields, in the Before a task is Assigned Status of level 2 approval (This is the key here if you will update these task fields, it will lead to execution of Workflow. So just set it as shown in below Screen Shot). It is compulsory to create two  Task fields to store these values.



No as we have these values in our Task List fields so we can use them in our Info path form(Which is our Task Form as well). Because Info path form are by default associated with Task List fields.

In the info path form in what ever field we want to show this value we need to bind that field with our  Assigned fields. So they will display data entered at first level of approval. I

Step 3: As we got these values in our Info Path form so we need to edit our second level Task Form using info path form client and use some basic info path modification techniques to get it done.
a) In the Approve button of info path we need to set a rule that current user can not be same as what value is in the FirstApprover field (which we have set in this field before a task is assigned so the first app-rover value is with us already).

 We need to set very same rule in Reassign task or any action in which we want to check that First Approver should not perform this action. Using the value of First Approver Field and with new value entered or current user. There is a very important point here to note that always save and return values in same format as Name or User ID or something.

Hope it help someone. Will attach compare Screen Shot for First/Second Approver later.