Friday, 2 January 2015

How create strong name for third part .dll files.

We are facing "Strong name not found" issue while using third part dll in our solution.  The resolving steps as below.

    For example third party dll is sample.dll  and key file in yourkey.snk
  1. Open Visual studio Command prompt.
  2. Navigate to the third party dll folder 
  3. Execute below command will create Key file.
                    sn -k Yourkey.snk
     4.  Disassemble the dll file by using below command
          ildasm /all /out=sample.il sample.dll
5. Rebuild and Sign the dll
ilasm /dll /key=YourKey.snk sample.il

Happy coding :)


             

Saturday, 1 November 2014

Get Time Difference like Facebook Post 30 min ago 50 ago 1 hour ago or October 23

function getTimeString(givenDate) {

var monthNames = new Array();

monthNames[0] = "January";

monthNames[1] = "February";

monthNames[2] = "March";

monthNames[3] = "April";

monthNames[4] = "May";

monthNames[5] = "June";

monthNames[6] = "July";

monthNames[7] = "August";

monthNames[8] = "September";

monthNames[9] = "October";

monthNames[10] = "November";

monthNames[11] = "December";

var previousDate=new Date(givenDate);

var currentDate=new Date();

var milliSeconds=currentDate.getTime()-previousDate.getTime();

var daysDifference = Math.floor(milliSeconds/1000/60/60/24);

var timeDiffernceText="";




 
if( daysDifference ==0)



{

milliSeconds -= daysDifference*1000*60*60*24
 
var hoursDifference = Math.floor(milliSeconds/1000/60/60);

if(hoursDifference==0)



{

milliSeconds -= hoursDifference*1000*60*60
 
var minutesDifference = Math.floor(difference/1000/60);



timeDiffernceText=minutesDifference.toString();
 
timeDiffernceText=timeDiffernceText + " Minutes";



}
 
else



{

timeDiffernceText=hoursDifference.toString();
 
timeDiffernceText=timeDiffernceText+" Hours";



}

}
 
else



{
 
var month=previousDate.getMonth();



timeDiffernceText=monthNames[month];
 
timeDiffernceText=timeDiffernceText+" "+ previousDate.getDate().toString();



}
 
return timeDiffernceText;



}

Monday, 22 April 2013

How to get list item by absolute URL of the item

Pass the URL as query string named DocumentPath.


  protected void Page_Load(object sender, EventArgs e)
        {
            string documentPath = Convert.ToString(Request.QueryString["DocumentPath"]);
            if (string.IsNullOrEmpty(documentPath))
            {
            }
            else
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = site.RootWeb)
                        {

                            SPListItem item = web.GetListItem(documentPath);
                        }
                    }
                });


            }


        }

            }

        }

Wednesday, 17 April 2013

How to create webpart page programatically in SharePoint 2010 (Source Code)

/// <summary>
/// Create new webpart page
/// </summary>
/// <param name="list">List to keep page</param>
/// <param name="pageTitle">Page title</param>
/// <param name="layoutTemplate">Layout template id</param>
/// <returns>Operation result</returns>
private static string CreateWebPartPage(SPList list, string pageTitle, int layoutTemplate)
{
    const string newItemTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                    "<Method ID=\"0,NewWebPage\">" +
                                    "<SetList Scope=\"Request\">{0}</SetList>" +
                                    "<SetVar Name=\"Cmd\">NewWebPage</SetVar>" +
                                    "<SetVar Name=\"ID\">New</SetVar>" +
                                    "<SetVar Name=\"Type\">WebPartPage</SetVar>" +
                                    "<SetVar Name=\"WebPartPageTemplate\">{2}</SetVar>" +
                                    "<SetVar Name=\"Overwrite\">true</SetVar>" +
                                    "<SetVar Name=\"Title\">{1}</SetVar>" +
                                    "</Method>";
    var newItemXml = string.Format(newItemTemplate, list.ID, pageTitle, layoutTemplate);
    return list.ParentWeb.ProcessBatchData(newItemXml);
}

Using:
var web = SPContext.Current.Web;
var pagesLibrary = web.GetList("Pages");
CreateWebPartPage(pagesLibrary, "MyPage", 2);
Values for LayoutTemplate parameter:
  1. Full Page, Vertical
  2. Header, Footer, 3 Columns
  3. Header, Left Column, Body
  4. Header, Right Column, Body
  5. Header, Footer, 2 Columns, 4 Rows
  6. Header, Footer, 4 Columns, Top Row
  7. Left Column, Header, Footer, Top Row, 3 Columns
  8. Right Column, Header, Footer, Top Row, 3 Columns

Thursday, 17 January 2013

Code to create column in the existing document library



public static void AddCustomDocumentLibraryColumn(string documentLibraryName, string columnName,string columnDisplayName,string columnDescription, SPFieldType columnDataType)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPDocumentLibrary docLibrary = null;
                            if (web.TryGetDocumentLibrary(documentLibraryName, out docLibrary))
                            {

                                if (!docLibrary.Fields.ContainsField(columnName))
                                {
                                    bool allowUnsafeUpdateState = web.AllowUnsafeUpdates;
                                    web.AllowUnsafeUpdates = true;
                                    SPView defaultView = docLibrary.DefaultView;
                                  
                                    SPField libField = docLibrary.Fields.GetField( docLibrary.Fields.Add(columnName, columnDataType, true));
                                    libField.Description = columnDescription;
                                    libField.Title = columnDisplayName;
                                    libField.Required = true;
                                    libField.Update();
                                    defaultView.ViewFields.Add(libField);
                                    defaultView.Update();
                                    docLibrary.Update();
                                    web.Update();
                                    web.AllowUnsafeUpdates = allowUnsafeUpdateState;
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {

            }
        }


TryGetDocumentLibrary is an extended function of the Web.

public static bool TryGetDocumentLibrary(this SPWeb web, string documentLibraryName,
                                                                       out SPDocumentLibrary documentLibrary)
        {

            bool isDocumentLibraryExist = false;
            web.AllowUnsafeUpdates = true;

            if (true == web.Properties.ContainsKey(documentLibraryName))
            {

                try
                {

                    documentLibrary = web.Lists[new Guid(web.Properties[documentLibraryName])]
                                                                                    as SPDocumentLibrary;
                    isDocumentLibraryExist = true;

                }
                catch
                {

                    web.Properties[documentLibraryName] = null;
                    web.Properties.Update();

                    TryGetDocumentLibrary(web, documentLibraryName, out documentLibrary);

                }

            }
            else
            {

                documentLibrary = (from SPList list in web.Lists
                                   where list.RootFolder.Name.Equals(documentLibraryName,
                                                            StringComparison.InvariantCulture)
                                   select list).FirstOrDefault() as SPDocumentLibrary;

                if (documentLibrary != null)
                {

                    web.Properties.Add(documentLibraryName, documentLibrary.ID.ToString("B"));
                    web.Properties.Update();
                    isDocumentLibraryExist = true;
                }

            }

            web.AllowUnsafeUpdates = false;

            return isDocumentLibraryExist;

        }