Making Read-Only Fields In Datasheet View

Home Forums General discussion Making Read-Only Fields In Datasheet View

Viewing 1 reply thread
  • Author
    Posts
    • #34927
      SteveE
      Participant

        Here’s some code I’ve been working on for quite some time. It’s probably not something a ton of people need, but if you do it was hard to find.

        Use case: You have a datasheet view in a classic web part page and only want certain fields to be editable.

        Setup:

        • Create a notepad file with the code below and save it something like “datasheetlock.js”
        • Upload the file to your Site Assets library
        • Go into page edit mode on your classic web part page and edit the datasheet web part
        • Under Miscellaneous/JS Link add this link “~sitecollection/SiteAssets/datasheetlock.js”. Click OK to save.

        Here’s the code for your datasheetlock.js file:

        
        
        var fieldContext = {
        	Templates:{
        	Fields: {
        	FieldNameToMakeReadOnly: {
        	View: function(ctx, b) 
        		{
        		b.AllowGridEditing = false;
        		return ctx.CurrentItem.FieldNameToMakeReadOnly;
        		}     
        	}
        	}
        	}
        };
        
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext); 

        If you have a people picker, you have to do it a little differently:

        
        
        var fieldContext = {
        	Templates:{
        	Fields: {
        	PeoplePickerField: {
        	View: function(ctx, b) 
        		{
        		b.AllowGridEditing = false;
        		return "<span>" + ctx.CurrentItem.PeoplePickerField[0].title + "</span>"; 
        		}     
        	}
        	}
        	}
        };
        
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext); 
      • #34957
        Alexander Bautz
        Keymaster

          Thanks for sharing – nice tip for users working in classic list views.

          Alexander

      Viewing 1 reply thread
      • You must be logged in to reply to this topic.