Tuesday 7 February 2017

SPForm How to get a hidden field value on edit form




We can't directly get the value on edit form. So first we need to add the column in Edit form and on its property window hide them via CSS by setting the Style property in ‘display: none’:




 

SPForm GetCurrentItemID on edit form

//Current web
var currentURL=GetUrlKeyValue('Source');
//Current ItemID
var CurrentItemID=GetUrlKeyValue('ID');

//Lookup field  value
var cmp=fd.field('Company').control()._el().find('select option:selected').text();

Set Value to a SingleLineText Field

fd.field('CompanyName').value(cmp);

How to set read only for field in JSeditor

fd.field('Company').readonly(true);

Friday 3 February 2017

SPForm Custom fields change function not triggered

fd.field('Project1').control()._el().find('select').change(function() {
  // your code here
});
 
fd.field('Project1').control()._el().find('select option:selected').text();

Rest Form Settings by programmatically

using (SPSite site = new SPSite("SiteURL"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList projectGeneral = web.Lists["ListName"];
                    for (int i = 0; i < projectGeneral.Fields.Count;i++ )
                        try
                        {
                            Console.WriteLine(projectGeneral.Fields[i].Title);
                            projectGeneral.Fields[i].ShowInNewForm = null;
                            projectGeneral.Fields[i].ShowInDisplayForm = null;
                            projectGeneral.Fields[i].ShowInEditForm = null;
                            projectGeneral.Fields[i].ShowInViewForms = null;                          
                            projectGeneral.Fields[i].Update();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error  : " + ex.Message);
                        }
                    Console.WriteLine("Press Enter Key");
                    Console.ReadLine();
                }
            }

Remove SPForm from a list's New/Edit form


Open List in SharePoint Designer


Lists-->contenttype--> clear form url of edit/new.

Then goto Form Settings and Select Fields in there

SPForm Loading gig on Saving

Insert the following code into JS-editor
 
fd.onsubmit(function() {
     SP.UI.ModalDialog.showWaitScreenWithNoClose('Processing...', 'Please wait while request is in progress...', 150, 330);
     return true;
});

Set Person or group field via script


<script src="/jquery-1.10.2.min.js" type="text/javascript"></script> 
<script src="/jquery.SPServices-2014.02.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
(function () {
    var ctx = {};
    ctx.Templates = {};
    ctx.Templates.Fields = {
        'ProjectResponsible': {
            'NewForm': renderAssignedTo
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();
function renderAssignedTo(ctx) {

     var currentUser = $().SPServices.SPGetCurrentUser({fieldNames: ["Title", "Name"]});   //get current user properties
alert(currentUser.Title);
     var currentUserEntry = createUserEntity(currentUser.Name,currentUser.Title);
     //Set user default value
     ctx.CurrentFieldValue = [];   //Note: it is assumed the user field is a multi-valued field (!)
     ctx.CurrentFieldValue.push(currentUserEntry); 
     return SPClientPeoplePickerCSRTemplate(ctx);
}
function createUserEntity(userLoginName,userDisplayName)
{
   return {
      Description: userLoginName,
      DisplayText: userDisplayName,
      EntityGroupName: "",
      EntityType: "",
      HierarchyIdentifier: null,
      IsResolved: true,
      Key: userLoginName,
      MultipleMatches: [],
      ProviderDisplayName: "",
      ProviderName: ""
   };
}
</script> 

Thursday 2 February 2017

How to get sharepoint list data on sql by calling content db


Create a view for accessing list items in db



/*lsitid is GUID of list name */


ALTER  View [dbo].[ViewName] AS   /*Say Vehicle */
 SELECT 
  nvarchar1 as VehicleName,
  CAST(tp_ID AS INT) as ID,
  int2 as VehicleCategory,
  float1 as SortOrder
  
FROM

 [WSS_Content_siteName].[dbo].[AllUserData] AS list

WHERE

 list.tp_iscurrentversion=1 AND list.tp_RowOrdinal=0

AND list.tp_CalculatedVersion=0 AND list.tp_DeleteTransactionId=0x AND

    list.tp_listid='88443F36-7C2F-4779-958F-56C38B9923423'

If you did any changes on list item and you need to select only the approved changes then you must add another filter condition and tp_ModerationStatus=0

Thus


/*lsitid is GUID of list name */
ALTER  View [dbo].[ViewName] AS   /*Say Vehicle */
 SELECT 
  nvarchar1 as VehicleName,
  CAST(tp_ID AS INT) as ID,
  int2 as VehicleCategory,
  float1 as SortOrder
  
FROM
 [WSS_Content_siteName].[dbo].[AllUserData] AS list
WHERE
 list.tp_iscurrentversion=1 AND list.tp_RowOrdinal=0
AND list.tp_CalculatedVersion=0 AND list.tp_DeleteTransactionId=0x AND
    list.tp_listid='88443F36-7C2F-4779-958F-56C38B9923423'
and tp_ModerationStatus=0



Suppost the VehicleACtegory is a look up field with multiple selection then you will not get that value on the above query. In order to select the lookupdata
  you need the below query


/* Here AUDJ.tp_FieldId is the GUID of VehicleCategory field in Vehicle List and AUD.tp_listid is GUID of vehicle list */
CREATE  View [dbo].[VehicleCategory] AS
 SELECT 
AUD.tp_ID as ID,
  nvarchar1 as Title,
  CAST(AUDJ.tp_ID AS INT) as [VehicleCategory],
  float1 as SortOrder
   FROM
[WSS_Content_siteName].[dbo].[AllUserDataJunctions] AUDJ, [WSS_Content_siteName].[dbo].[AllUserData] AUD
  WHERE
 AUDJ.tp_iscurrentversion=1 AND AUDJ.tp_CalculatedVersion=0 AND AUDJ.tp_DeleteTransactionId=0x
 and AUDJ.tp_FieldId like '6983dc8a-7e23-41d8-a8ba-ee53339c2bb7'
 AND AUD.tp_listid='88443F36-7C2F-4779-958F-56C38B994B78'
 AND AUD.tp_iscurrentversion=1 AND AUD.tp_rowOrdinal=0 AND AUD.tp_CalculatedVersion=0 AND AUD.tp_DeleteTransactionId=0x
 AND AUDJ.tp_DocId = AUD.tp_DocId
 





 
 
 

Wednesday 1 February 2017

Set Person or group filed with current user using javascript

<script src="/jquery-1.10.2.min.js" type="text/javascript"></script> 
<script src="/jquery.SPServices-2014.02.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
(function () {
    var ctx = {};
    ctx.Templates = {};
    ctx.Templates.Fields = {
        Field Display Name': {
            'NewForm': renderAssignedTo
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();
function renderAssignedTo(ctx) {
     var currentUser = $().SPServices.SPGetCurrentUser({fieldNames: ["Title", "Name"]});   //get current user properties
     var currentUserEntry = createUserEntity(currentUser.Name,currentUser.Title);
     //Set user default value
     ctx.CurrentFieldValue = [];   //Note: it is assumed the user field is a multi-valued field (!)
     ctx.CurrentFieldValue.push(currentUserEntry); 
     return SPClientPeoplePickerCSRTemplate(ctx);
}
function createUserEntity(userLoginName,userDisplayName)
{
   return {
      Description: userLoginName,
      DisplayText: userDisplayName,
      EntityGroupName: "",
      EntityType: "",
      HierarchyIdentifier: null,
      IsResolved: true,
      Key: userLoginName,
      MultipleMatches: [],
      ProviderDisplayName: "",
      ProviderName: ""
   };
}
</script>