Wednesday, October 2, 2013

SharePoint 2013 : The server was unable to save the form at this time. Please try again.

I have a project site created and for some strange reason, I am not able to create any items in any list. I did everything mentioned in many of the blog posts online but nothing worked.
Fianlly, I found this blog today and by enabling the web site with anonymous authenticated, it worked. I have no idea what is going on.


Update: 02.13.2014
The following fix is also required for me---> read here

Thursday, September 12, 2013

SharePoint : SPGridView - Collapse groups by default

If using SPGridView and groups need to be collapsed by default when the page is loaded, place a script editor webpart below the grid and copy the following between "script" tags
var trRows = document.getElementsByTagName('tr');
var countRows = trRows.length;
for (var i = 0; i < countRows; ++i) 
{
    if (trRows[i].getAttribute('isexp') != null && trRows[i].getAttribute('isexp').toLowerCase() == 'true')
    {
        if(trRows[i].firstChild.firstChild != null && trRows[i].firstChild.firstChild.tagName.toLowerCase() == 'a')
        {  
            {
                trRows[i].firstChild.firstChild.fireEvent('onclick');
            }
        }
    }
}

Wednesday, September 11, 2013

SharePoint: Disable certain columns on New and Edit forms for few users

Recently, we had a requirement where in certain status fields for some lists are enabled for only few users when users create new items or edit existing columns.
Here is what we did to achieve this.
On both new and edit forms for the list, add a "Script Editor" web part. This web part allows us to write some jscript or jQuery as part of the page. Idea is to use SharePoint Client Object Model to access fields based on the current user. If the user is within the group specified we do not do anything otherwise disable the 'Status' field on the forms.
Here is the code to put inside "Script" tag.
SP.SOD.executeOrDelayUntilScriptLoaded(retrieveAllUsersInGroup, "SP.js");
function retrieveAllUsersInGroup() {
    var siteUrl = 'Replace site url here';
    var clientContext = new SP.ClientContext(siteUrl);
    var collGroup = clientContext.get_web().get_siteGroups();
    var oGroup = collGroup.getById(replace group id as integer);
    this.collUser = oGroup.get_users();
    currentUser = clientContext.get_web().get_currentUser();
    clientContext.load(currentUser);
    clientContext.load(collUser);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
    var userEnumerator = collUser.getEnumerator();
    var found = false;
    while (userEnumerator.moveNext()) {
        var oUser = userEnumerator.get_current();
        if (currentUser.get_title() == oUser.get_title())
        {
            found = true;
            break;
        }
    }
    if (!found)
    {   $("select[title='Status']").attr("readonly","true").css('background-color','lightgray');
        $("select[title='Status']").attr("disabled",true);  
    }
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}


NOTE: Make sure to replace site url and group id in the above code. And the field disabled is 'Status' dropdown.

Monday, September 9, 2013

SharePoint 2013: SPGridView does not render correctly

I was required to group columns programmatically in SharePoint 2013. There were couple of issues when Paging and Grouping enabled together. Even though the following article is 4 years old, this still seems to the solution and worked for me.
http://blogs.msdn.com/b/valdon/archive/2009/03/11/how-to-work-around-bugs-in-the-spgridview-control.aspx

Wednesday, September 4, 2013

SharePoint 2013: Links on Publishing Page do not work due to MDS (Miniaml Download Strategy)

We have a published page with a web part on it. Links inside this web part do not seem to work when the page is in "Published" mode. They do work as expected in edit mode when page is checked out and unpublished.
The reason is MDS (Minimal Download Strategy). Even if the feature is disabled script files in SharePoint 2013 are loaded only when required. Specially sp.js file.
So here is the solution. Put a script editor webpart on top of the controls and have the following script and then publish the page within a "script" block.
var clientContext;
var website;
// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
// Create an instance of the current context.
function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    website = clientContext.get_web();
    clientContext.load(website);
    clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded() {
 }
function onRequestFailed(sender, args) {
 }