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);
};
}


Converting InfoPath data connections to UDCX files

While working with InfoPath 2013 and SharePoint 2013 on a Windows 2012 platform, I came across an issue where I was not able to convert the InfoPath data connections into UDCX files. I was getting the error below:

The specified location does not exist or could not be opened. Choose a data connection library on a server running Microsoft SharePoint Server and specify a valid filename.


After some read through, I found out that for this to be able to work, I would need a Windows feature installed on the server. This feature is Desktop Experience. Once I enabled this from the Server Manager, and restarted the server, I got rid of this error. Files were then getting converted to UDCX and were published to the Data Connection library.



Hope this helps someone.

Disabling mobile site within SharePoint 2010

As we all know that SharePoint creates a very basic (ugly looking) default site for mobile devices. When opening the site from any mobile browser, it automatically directs the website to the respective mobile website which has /m appended to it.
My client did not wanted the default mobile site to open within mobile devices as it gives a very untidy look so they wanted to disable the redirect function. If you have similar issues, follow the steps below to disable the redirection.
  • Navigate to:
    • C:\inetpub\wwwroot\wss\VirtualDirectories\<yoursharepointsite>\App_Browsers\compat.browser
  • Locate
    • <userAgent match=”iPhone”&gt
  • Now under tag, locate
    • <capability name=”isMobileDevice”    value=”true”/>
  • Change this to false
  • Now for Android, repeat the same thing and set the ismobileDevice attribute to false.

No need to reset the iis or anything else. Just save the file and browse the site from iphone, you will see the desktop site opening.

Also, I noticed that if you create a backup copy of the file with any name within the folder, the site breaks and doesn’t render. So make changes within the file and copy it to some other location for a backup copy.

There is a MobilityRedirect feature within the SPWeb which can be disabled to prevent the redirection as well.
  • Disable-SPFeature –Identity MobilityRedirect –Url

Hope this helps someone

Deleting work item in TFS using Visual Studio 2010

As I was setting up TFS (Team Foundation Services) for my upcoming SharePoint project, I created few work items such as Tasks, Sprint Backlog, Bugs etc. also created custom fields and values within them. To test these items, I created a few test work items before moving it to live. Now, I wanted to get rid of the test work items I created before I could log live data in it. So here comes this post, where I would write on how we can delete work items from TFS. So, first of all, there is no simple tick and delete UI from Microsoft which does so. Microsoft recommends using command line to delete the work items. There are some free tools available through few of the sites, but I prefer deleting using command line. Follow the steps below:
  • Open Visual Studio Command Prompt by navigating to Start > All Programs > Microsoft Visual Studio 2010 > Visual Studio Tools
  • Note down the IDs of the work items you want to remove from the TFS
  • Within the command prompt type the below syntax:
witadmin destroywi /collection /id:,,


Note: For noting down the IDs of the work items you can run a query that has ‘test’ in the Title field, provided you have entered test while your testing.

Resetting Sync databse within SharePoint

Resetting Sync database will delete the database completely with all the values in it.
I think one should reset the sync database as the last thing when all troubleshooting tricks provide no result. Only consider resetting when: 
  • Your import user profile connection is fetching details from AD but they cannot be seen on SharePoint
  • Similar scenario with export connections, so you want to push changes back to AD but you don’t see any changes on AD
  • You have modified settings on your FIM Client manually 

Here is a very nice article on resetting the sync database you should follow before considering a  reset:
PS: The User related information such as Profile Pictures, About Me etc. resides in Profile DB and the Social DB, these information will be safe as you do a reset of your Sync database

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();