Wednesday, November 5, 2014

Breaking Item level Permission on ItemAdded and Add a Specific Group (Permission) for item

Here is the code for breaking permission for an item, whenever Items get added. And you can also add any group or permission as per your need. It can be very useful at times :

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace DemoByKrishna.BreakItemLevelPermisiion
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class DocReadAccessEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            this.EventFiringEnabled = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    using (SPSite site = new SPSite(properties.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                        {
                            //SPList list = properties.List;
                            //SPListItem item = properties.ListItem;
                            SPListItem item = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID);


                            web.AllowUnsafeUpdates = true;

                            item.BreakRoleInheritance(false);
                            string Group = "Viewers";
                            SPPrincipal cxosGroupUserGroup = FindUserOrSiteGroup(site, Group);
                            SPRoleDefinitionCollection GroupRole = web.RoleDefinitions;
                            SPRoleAssignment GroupRoleAssign = new SPRoleAssignment(cxosGroupUserGroup); 
                            cxosGroupRoleAssign.RoleDefinitionBindings.Add(cxosGroupRole["Read"]);
                            item.RoleAssignments.Add(GroupRoleAssign);

                            item.Update();
                        }
                    }
                }
                catch (Exception ex)
                {
                    properties.Status = SPEventReceiverStatus.CancelWithError;
                    properties.ErrorMessage = ex.Message;
                    properties.Cancel = true;
                }
            });
            this.EventFiringEnabled = true;

        }

        private static SPPrincipal FindUserOrSiteGroup(SPSite site, string userOrGroup)
        {
            SPPrincipal myUser = null;


            if (SPUtility.IsLoginValid(site, userOrGroup))
            {
                myUser = site.RootWeb.EnsureUser(userOrGroup);
            }
            else
            {
                foreach (SPGroup g in site.RootWeb.SiteGroups)
                {
                    if (g.Name.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == userOrGroup.ToUpper(System.Globalization.CultureInfo.InvariantCulture))
                    {
                        myUser = g;
                        break;
                    }
                }
            }
            return myUser;


        }

    }
}

Monday, July 14, 2014

Logging in SharePoint 2010 Custom Applications using SharePoint SPDiagnosticsServiceBase Class.

In SharePoint 2010 we have a very easy way to log errors in default SharePoint Log at "14\Log" folder by default. However if you are not able to view it here you can check Central Administrator --> Monitoring -->
Configure Diagonistic Logging -->Trace Log. Here is the code for :

Add  a class file into your project and then paste below code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;


namespace LoggingServiceExample
{
    public class LoggingService : SPDiagnosticsServiceBase
    {
        public static string vsDiagnosticAreaName = "SharePoint Logging Service";
        public static string CategoryName = "SharePointProject";
        public static uint uintEventID = 700; // Event ID
        private static LoggingService _Current;
        public static LoggingService Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new LoggingService();
                }
                return _Current;
            }
        }
        private LoggingService()
            : base("SharePoint Logging Service", SPFarm.Local)
        { }
        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
                 {
                  new SPDiagnosticsArea(vsDiagnosticAreaName, new List<SPDiagnosticsCategory>
                   {
                    new SPDiagnosticsCategory(CategoryName, TraceSeverity.Medium, EventSeverity.Error)
                   })
                  };
            return areas;
        }
        public static string LogErrorInULS(string errorMessage)
        {
            string strExecutionResult = "Message Not Logged in ULS. ";
            try
            {
                SPDiagnosticsCategory category = LoggingService.Current.Areas[vsDiagnosticAreaName].Categories[CategoryName];
                LoggingService.Current.WriteTrace(uintEventID, category, TraceSeverity.Unexpected, errorMessage);
                strExecutionResult = "Message Logged";
            }
            catch (Exception ex)
            {
                strExecutionResult += ex.Message;
            }
            return strExecutionResult;
        }
        public static string LogErrorInULS(string errorMessage, TraceSeverity tsSeverity)
        {
            string strExecutionResult = "Message Not Logged in ULS. ";
            try
            {
                SPDiagnosticsCategory category = LoggingService.Current.Areas[vsDiagnosticAreaName].Categories[CategoryName];
                LoggingService.Current.WriteTrace(uintEventID, category, tsSeverity, errorMessage);
                strExecutionResult = "Message Logged";
            }
            catch (Exception ex)
            {
                strExecutionResult += ex.Message;
            }
            return strExecutionResult;
        }
    }
}

You just need to call following code on each entry points for any custom component :

try
{
            
}
catch(Exception ex)
 {
LoggingService.LogErrorInULS("Custom SharePoint Application exception at " + System.DateTime.Now + "Exception Message : "+ ex.Message.ToString() +" Exception Stacktrace : "+ ex.StackTrace.ToString());
}

Hope it helps..!!

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...!!