Monday 27 August 2018

Get Maximum ID of a SharePoint List/Library



I have googled a lot for finding a simple method that will give the maximum ID of a SharePoint list/library. But I have found lots of complex codes and those are also large in lines. I was very astonished that it should be a very simple task. Then I have tried it in my way and it was working:



 private int GetMaxID()
        {
            SPList listName = spWeb.Lists["ListName"];
            int maxID = 0;

            SPListItemCollection listCollection = listName.Items;

            foreach (SPListItem item in listCollection)
            {
                maxID = Convert.ToInt32(item["ID"]);
            }
                
            return maxID;
        }  


It was so simple. Isn't it...........!!

Monday 7 May 2018

On Enter Key Press in any Text Field Visual Web Part/Custom Web Part is going to Edit Mode in SharePoint 2016


It is a common problem with SharePoint 2016 Web Parts. If user press Enter key in any text field in any visual web parts, the navigation bar is going to the Edit Mode and user can edit or delete the sensitive properties of the web part.


This is a huge problem for some business case. Someone telling it is a bug of SharePoint, someone advising to handle it from the server side for every text field but neither seems to be potential solution to me.

Finally, I thought to track the Enter Key press for the whole page and write down a JQuery event to track it and prevent the default action.

So, the solution is to add a Content Editor web part above the Visual Web Part and add the  script as shown below:




Click On Edit Source as shown above add the below script:


<script src="/_layouts/16/Content/js/jquery-1.10.2.min.js"></script>

<script type="text/javascript">

$(document).keypress(function(e) 
{
    if (e.which == 13) 
{
    // don't do a "edit" post default! 
   event.preventDefault();
  
   return false;
    }
});
</script>​​

The Screenshots shows the details:





Press Ok and Save the Page. Hopefully the problem is disappeared.


Happy SharePointing...............!!