Override redirect using EditItemWithCheckoutAlert

On an edit item link, you want to perform some operation and redirect to a page of your choice, write a small script on your page and override the EditItemWithCheckoutAlert function.
So when you click on the edit link, the EditForm.aspx opens.


This allows you to modify the fields and delete the list item. Once you perform your desired action, the dialog box is closed and you are redirected to previous view/default view. To override this, use the script below, modify it to suit your URL and put it in a CEWP/Script Editor WP on your page.




if (typeof(EditItemWithCheckoutAlert) != 'undefined')
{
var changed_EditItemWithCheckoutAlert = EditItemWithCheckoutAlert;
EditItemWithCheckoutAlert = function(evt, Url, bCheckout, bIsCheckedOutToLocal, strDocument, strhttpRoot, strCurrentUser, strCheckoutUser) {
changed_EditItemWithCheckoutAlert(evt, Url+'&Source=yourURL', bCheckout, bIsCheckedOutToLocal, strDocument, strhttpRoot, strCurrentUser, strCheckoutUser);
};
}


Using JQuery to hide SharePoint 2013 List Item fields

Hiding unnecessary fields in DispForm.aspx/EditFOrm.aspx is something most of us implement within our SharePoint implementations. Here is a quick way to achieve that using JQuery.
Hiding any List Item fields
So you have many fields in your NewForm.aspx but you want to hide some like in the screenshot. I want to hide the fields I haven’t underlined.

I will click on Edit Page, and add a Script  Editor webpart on the page. From the below script, just change the number in which your fields appear on the page using a 0 index. So to hide the first field, enter 0, second field – enter 1 and so on.


$(document).ready( function() {
$("TABLE.ms-formtable>TBODY>TR:eq(3)").css('display', 'none');
$("TABLE.ms-formtable>TBODY>TR:eq(7)").css('display', 'none');
$("TABLE.ms-formtable>TBODY>TR:eq()").css('display', 'none');
$("TABLE.ms-formtable>TBODY>TR:eq(9)").css('display', 'none');
$("TABLE.ms-formtable>TBODY>TR:eq(10)").css('display', 'none');
})

Also you can do some formatting on your page using Jquery, for example I am making the font as BOLD for the list item entries.

$(".ms-formbody").css("font-weight","bold");

Also,
I want to hide the information in the screenshot, you can add this line as well.

$(".ms-descriptiontext").css("display","none");

Hope this helps someone looking for a similar scenario.

Retrieving SharePoint List Items using Javascript in a Custom Webpart

So you want to fetch list items but do not want any post back, yes we can implement that using Javascript within our custom webpart by using a client control. 

Let me explain that in a step by step process
  • Create a new Visual Webart Project in VS 2012.
  • Give it a proper name and make it a Farm level solution.
  • Now delete the existing Visual WebPart item within the solution (to remove the default name) and add a new item – Visual WebPart and give it the desired name.
  • Open ascx file and copy paste the code below









Test


function fetchListItems()
{
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('List Name');
//I am fetching the list items randomly from the first 30 items
var randomNumber = Math.floor((Math.random() * 30) + 1);
var camlQuery = new SP.CamlQuery();

//increase the number within the RowLimit tag to fetch the number of items you wish
camlQuery.set_viewXml('' +
'' + randomNumber + '1');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded(sender, args) {

var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title') +
'\nBody: ' + oListItem.get_item('Body');

}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Here I am using client control and not any server control so there will not be any post back experience.
If you wish you can add more functionality to this same webpart and use it in your real world scenario like adding server controls to it and then writing some code behind for it and so on.

Programatically breaking inheritance and adding unique permission to List Items

Permission handling is something which makes SharePoint a favorite product within the industry. SharePoint has Permission levels in built for basic roles, however you can create your own permission levels and assign to Groups and then set those groups to access any site/library/list. Whenever a sub-site is created, it asks you if you want to inherit the permission from the parent site or want to create unique permissions. 

If you choose to inherit the permissions, it will copy the same set of permissions to the sub-site which will again be passed on to the lists/libraries you create. It may happen that for securing our content, we want to break this inheritance and want to assign unique permissions to restrict the users for certain actions and to make the content more secure. Inheritance can be broken manually using the Out of feature also and if you have a business logic in place, you can do it programmatically also. This blog post gives you code to break inheritance for items meeting certain criteria and assigning permissions to them explicitly. I am using ItemAdded() event receiver to do this. You can use it within workflow or any custom webpart as well.

SPGroup securityGroup = spWeb.SiteGroups["Security Group Name"]; 
SPRoleDefinition groupRole = spWeb.RoleDefinitions["Read"];

SPRoleAssignment roleAssign = new SPRoleAssignment(securityGroup);
roleAssign.RoleDefinitionBindings.Add(groupRole);

SPListItem listItem = spWeb.GetListItem("http://List Item URL");
listItem.BreakRoleInheritance(true);
listItem.RoleAssegnments.Add(roleAssignment);
listItem.Update();