How to set internal names of new SharePoint fields with PowerShell? - powershell

I'm trying to add a new field to a SharePoint list and set its internal name. I have both the static name and display name specified but I can't figure out why the internal name gets set as First_x0020_Name.
$web = Get-SPWeb 'https://server'
$list = $web.Lists['listName']
$newFieldXML = '<Field Type="Text" StaticName="FirstName" DisplayName="First Name"></Field>'
$list.Fields.AddFieldAsXml($newFieldXML)

This simply works for me-
XML should be of format:
$newFieldXML = '<Field Type="Text" Name="FirstName" StaticName="FirstName" DisplayName="First Name"></Field>'
In PowerShell use [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldInternalNameHint as third parameter of AddFieldAsXml function
$spListFields = $spList.Fields.AddFieldAsXml($newFieldXML, $true, [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldInternalNameHint)

It looks like AddFieldAsXml cannot be used to set the internal name
According to the article, if you want to use AddFieldAsXml to set the internal name you need to:
Define your CAML so that the Internal Name you want to use is
actually set as the Display Name
Create the field with the call to AddFieldAsXml
After the field is created, retrieve it using the internal name and set the Title property to the real Display Name you wanted it to
be in the first place
You need two additional steps: retrieve the field and set the internal name

Use:
$list.Fields.AddFieldAsXml($newFieldXML, $true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldInternalNameHint)
instead of
$list.Fields.AddFieldAsXml($newFieldXML)
Refer this link

RESOLVED:
Please check below post for the Answer:
Set Field Internal Name For New Fields
Sample Code:
Please use [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
$fieldXml="<Field Type='Text' DisplayName='"+ $Field_DisplayName +"'
Required='"+$Field_Required+"' MaxLength='255'
StaticName='"+$Field_InternalName+"' Name='"+$Field_InternalName+"' />
$spoList.Fields.AddFieldAsXml($fieldXml,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$spoList.Update()
$clientContext.ExecuteQuery()

You can use this script to create fields with internal name and then update the display name.
$web = Get-SPWeb 'https://server'
$list = $web.Lists['listName']
$newFieldXML = '<Field Type="Text" StaticName="FirstName"
DisplayName="FirstName"></Field>'
$list.Fields.AddFieldAsXml($newFieldXML)
$fld = $list.Fields["FirstName"]
$fld.Title = "First Name"
$fld.Update();

You can't change the internalName of a field after his creation.
If you use XML to create field :
The internalName of field is set with the Attribute : "Name"
so your field XML defintion should be :
$newFieldXML = '<Field Type="Text" Name="FirstName" StaticName="FirstName" DisplayName="First Name"></Field>'

Related

ExtendedPropertyDefinition declaration for EmailMessage in Powershell throws exception

since I got to know that ExtendedProperties have its limit for a specific mailbox in the EWS cloud I am trying to switch up my code to have only one ExtendedProperty and just change its value each time I am assigning the property to an e-mail message I am sending to then find it and work on the e-mail message object later on in the program.
I am having a hard time setting this up correctly even though I am following the docs, but it just seems to not work out for me.
This is the code part that throws an Exception: "Multiple ambigious overloads found for "ExtendedPropertyDefinition" and the argument count "3" :
# email declaration exposing the $email object
.
.
.
# property declaration and setting the value
# since I want to have only one extended property, this is actually a valid GUID string that I then # convert to a Guid type
$GUIDproperty = "00000000-0000-0000-0000-000000000000"
$propertyGUID = [Guid]$GUIDproperty
# since I want to have a unique value each time set to the existing extended property
$propertyValue = [guid]::NewGuid().ToString()
$propertyName = "Id"
$ExtendedProperty = [Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition]::new($propertyGUID, $propertyName, $propertyType)
# well I dont even reach this part, but just for the big picture
$email.SetExtendedProperty($ExtendedProperty, $propertyValue)
The docs I have followed for that are the following:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.exchange.webservices.data.extendedpropertydefinition.-ctor?view=exchange-ews-api#microsoft-exchange-webservices-data-extendedpropertydefinition-ctor(microsoft-exchange-webservices-data-defaultextendedpropertyset-system-string-microsoft-exchange-webservices-data-mapipropertytype)
https://learn.microsoft.com/en-us/dotnet/api/microsoft.exchange.webservices.data.folder.setextendedproperty?redirectedfrom=MSDN&view=exchange-ews-api#Microsoft_Exchange_WebServices_Data_Folder_SetExtendedProperty_Microsoft_Exchange_WebServices_Data_ExtendedPropertyDefinition_System_Object_
https://learn.microsoft.com/en-us/dotnet/api/system.guid?view=net-7.0
The following works okay for me
$propertyType = [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String
$GUIDproperty = "82e3d64f-e26d-4321-8fc3-c31aa790197c"
$propertyGUID = [Guid]$GUIDproperty
$propertyValue = [guid]::NewGuid().ToString()
$propertyName = "MyPropId"
$ExtendedProperty = [Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition]::new($propertyGUID, $propertyName, $propertyType)
return $ExtendedProperty
You don't specify what you using in the $propertyType so that maybe it, could also be to do with the versions you using. What version of PowerShell and the EWS Managed API are you trying ?

How to replace/update the Value of an Attribute in LDAP Directory using PowerShell?

The entries in our companys Non-AD LDAP Server look like this:
uid = e145871
sn = Smith
givenName = John
department = Research & Development
department = Human Resource
And so on...
I've developed a PowerShell script to add specific attributes and values which is working just fine. Now I need to replace specific values but the issue is the identical attribute name. (In this case it's "department")
My goal is to replace "Research & Development" with "Something Else". If I run the following script it gets replaced but Human Resource is deleted as well. Is it possible to replace only one value without touching/deleting the other?
$r = New-Object -TypeName System.DirectoryServices.Protocols.ModifyRequest
$r.DistinguishedName = "uid=e145871,ou=identities,ou=users,o=items,dc=company,dc=domain,dc=com"
$DirectoryRequest_value = New-Object "System.DirectoryServices.Protocols.DirectoryAttributeModification"
$DirectoryRequest_value.Name = "department"
$DirectoryRequest_value.Contains("Research & Development")
$DirectoryRequest_value.Operation = [System.DirectoryServices.Protocols.DirectoryAttributeOperation]::Replace
$DirectoryRequest_value.Add("SomethingElse")
$r.Modifications.Add($DirectoryRequest_value)
$result = $connection.SendRequest($r)
Thanks!
The LDAP Replace operation replaces (or overwrites) the entire value of the attribute, including any existing values that might exist as part of a multi-valued attribute.
From RFC4511 ยง4.6 - "Modify Operation":
- operation: Used to specify the type of modification being
performed. Each operation type acts on the following
modification. The values of this field have the following
semantics, respectively:
[...]
replace: replace all existing values of the modification
attribute with the new values listed, creating the attribute
if it did not already exist. A replace with no value will
delete the entire attribute if it exists, and it is ignored
if the attribute does not exist.
Instead, add two separate modifications to the request - one to add "SomethingElse" and one to remove "Research & Development":
$targetObject = 'uid=e145871,ou=identities,ou=users,o=items,dc=company,dc=domain,dc=com'
$attributeName = 'department'
$oldValue = 'Research & Development'
$newValue = 'SomethingElse'
$request = [System.DirectoryServices.Protocols.ModifyRequest]::new()
$request.DistinguishedName = $targetObject
# This modification will add the new value "SomethingElse"
$addNewDepartment = #{
Name = $attributeName
Operation = 'Add'
} -as [System.DirectoryServices.Protocols.DirectoryAttributeModification]
$addNewDepartment.Add($newValue) |Out-Null
$request.Modifications.Add($addNewDepartment) |Out-Null
# This modification will remove the old value "Research & Development"
$removeOldDepartment = #{
Name = $attributeName
Operation = 'Delete'
} -as [System.DirectoryServices.Protocols.DirectoryAttributeModification]
$removeOldDepartment.Add($oldValue) |Out-Null
$request.Modifications.Add($removeOldDepartment) |Out-Null
$result = $connection.SendRequest($request)

How to use Sitename in Constants as a default value in TYPO3?

I want to set the Sitename in Constants by default, so I can use this settings.variable in my Fluidtemplate.
I found in another post here on stackoverflow:
DB:sys_template|1|title
GLOBAL:TYPO3_CONF_VARS|SYS|sitename
But if I use this in my constants.ts like this:
# cat=plugin.tx_rmnavigation/01_NaviSettings/a; type=string; label=testing sitetitle
testsitetitle = DB:sys_template|1|title
OR
# cat=plugin.tx_rmnavigation/01_NaviSettings/a; type=string; label=testing sitetitle
testsitetitle = GLOBAL:TYPO3_CONF_VARS|SYS|sitename
AND in my setup.ts:
testsitetitle = {$plugin.tx_rmnavigation.settings.testsitetitle}
I get only the text not the value of the "variable" see this picture Constant Editor...
How can I use the Sitename in Constants as a defaultvalue?
Edit
I forgot to say, perhaps it's important for this issue, I try this here in both files:
plugin.tx_rmnavigation {
settings {
..
}
}
You have to assign your constant to a content object's data property (see https://docs.typo3.org/typo3cms/TyposcriptReference/8.7/ContentObjects/Index.html and https://docs.typo3.org/typo3cms/TyposcriptReference/8.7/Functions/Stdwrap/Index.html#data) to get it resolved:
testsitetitle = TEXT
testsitetitle.data = {$plugin.tx_rmnavigation.settings.testsitetitle}
And I would prefer your second variant for the constant definition because it uses the value from the current template record:
# cat=plugin.tx_rmnavigation/01_NaviSettings/a; type=string; label=testing sitetitle
testsitetitle = GLOBAL:TYPO3_CONF_VARS|SYS|sitename
But the first one should also work if you use colons instead of pipes:
testsitetitle = DB:sys_template:1:title
If you have a multi domain page the query to DB:sys_template:1:sitetitle might not work, as the 1 is the UID, not the PID of the root node of your template. But TSFE to the rescue!
In the context of your page call, the TSFE already has the sitetitle from the backend template loaded.
If you for example want to output a og:site_name, you can access the value by using:
og:site_name = TEXT
og:site_name {
data = TSFE:tmpl|sitetitle
attribute = property
}
This way no additional database queries are needed and it will work on multi domain, multi root node pages.
Thanks for your suggestions. I found a solution with your infos.
Honestly I think this doesn't works in Constants, because the both methods are readonly.
So I found a working solution for my Issue: I need that Variable only for read in my Templates, so I create a new Typoscript File libs.ts and included this with:
# Include Libraries
<INCLUDE_TYPOSCRIPT: source="FILE: EXT:rm_navigation/Resources/Private/TypoScript/libs.ts">
in the /Configuration/TypoScript/setup.ts File.
The content of libs.ts is:
TSFE-Syntax
lib.sitename = TEXT
lib.sitename.data = GLOBAL:TYPO3_CONF_VARS|SYS|sitename
OR
DB-Syntax
lib.sitename = TEXT
lib.sitename.data = DB:sys_template:1:sitetitle
works both. I read that you use the colon-syntax for DB usage and the pipe-syntax for Global Variables.
To get this to Fluid use this Code:
<f:cObject typoscriptObjectPath="lib.sitename" />
I hope it helps Others who also has this Issue.

QBFC: Specify custom field data when adding new customer (Custom field already exists)

I am running some tests to specify a value for a custom field that is already setup in quickbooks.
I cana dd the customer, but I fail to add the custom field... I think I have an issue # "FullName" in the last DataExtMod line...
Any ideas on this? I get no specific error, just an hresult.
Dim custAdd As ICustomerAdd = qbMsgReq.AppendCustomerAddRq
Dim str_name As String = "Louis Pluto Test 2 - ZA Code"
Dim str_phone As String = "0764128111"
custAdd.Name.SetValue(str_name)
custAdd.Phone.SetValue(str_phone)
custAdd.Email.SetValue("email#addy.com")
custAdd.FirstName.SetValue("myname")
custAdd.LastName.SetValue("my lastname")
custAdd.fullname.setvalue()
'add custom ID field...
Dim MyDataExtMod As IDataExtMod
MyDataExtMod = qbMsgReq.AppendDataExtModRq
MyDataExtMod.OwnerID.SetValue("0")
MyDataExtMod.DataExtName.SetValue("ID NUMBER")
MyDataExtMod.DataExtValue.SetValue("0123456789012")
MyDataExtMod.ORListTxn.ListDataExt.ListDataExtType.SetValue(ENListDataExtType.ldetCustomer)
MyDataExtMod.ORListTxn.ListDataExt.ListObjRef.FullName.SetValue(str_name)
UPDATE
My XML Responce:
MyDataExt_resp.ToXMLString "<?xml version="1.0" ?> <QBXML> <QBXMLMsgsRs> <CustomerAddRs requestID="0" statusCode="0" statusSeverity="Info" statusMessage="Status OK"> <CustomerRet> <ListID>8000051D-1369767881</ListID> <TimeCreated>2013-05-28T21:04:41+02:00</TimeCreated> <TimeModified>2013-05-28T21:04:41+02:00</TimeModified> <EditSequence>1369767881</EditSequence> <Name>Louis Test User with ID2</Name> <FullName>Louis Test User with ID2</FullName> <IsActive>true</IsActive> <Sublevel>0</Sublevel> <FirstName>Louis</FirstName> <LastName>van Tonder</LastName> <Phone>0123456789</Phone> <Email>email#addy.com</Email> <AdditionalContactRef> <ContactName>Main Phone</ContactName> <ContactValue>0123456789</ContactValue> </AdditionalContactRef> <AdditionalContactRef> <ContactName>Main Email</ContactName> <ContactValue>email#addy.com</ContactValue> </AdditionalContactRef> <Balance>0.00</Balance> <TotalBalance>0.00</TotalBalance> <JobStatus>None</JobStatus> </CustomerRet> </CustomerAddRs> <DataExtModRs requestID="1" statusCode="3120" statusSeverity="Error" statusMessage="Object "ID NUMBER" specified in the request cannot be found. QuickBooks error message: This feature is not enabled or not available in this version of QuickBooks." /> </QBXMLMsgsRs> </QBXML> " String
This part is obviously interesting... the custom field is just "ID NUMBER" , should I reference it in some other way? Naming convention?
<DataExtModRs requestID="1" statusCode="3120" statusSeverity="Error" statusMessage="Object "ID NUMBER" specified in the request cannot be found. QuickBooks error message: This feature is not enabled or not available in this version of QuickBooks." /> </QBXMLMsgsRs>
The FullName you set in your DataExt should be the same as the FullName for the customer you're creating. Otherwise, you'll be telling QuickBooks to set the custom field for some other random object.
e.g. if this is the FullName of your customer:
Dim str_name As String = "Louis Pluto Test 2 - ZA Code"
Then you should use that same FullName in your DataExt:
' This is *wrong*:
MyDataExtMod.ORListTxn.ListDataExt.ListObjRef.FullName.SetValue("Myname mylastname")
' This is correct:
MyDataExtMod.ORListTxn.ListDataExt.ListObjRef.FullName.SetValue(str_name)

Form Validation: Select

I'm trying to display an error message if the select button in my form is not changed. It works fine for the rest but not the select, please help! and I know that the image wont work like that, I cant post images as a new member.
Html is:
<div id='first_name_error' class='error'><image code here></div>
<div><input type='text' name='first_name' id='first_name' placeholder="YOUR FIRST NAME*"></div>
Number of Guests:*<div id='guests_error' class='error'><img src='img/booking/error.png'></div>
<div><select name='guests' id='guests' style="margin:0px;" SIZE="1"><OPTION SELECTED value="guests">Guests<OPTION>2<OPTION>3<OPTION>4</SELECT></div>
Code Is
var error = false;
var first_name = $('#first_name').val();
var second_name = $('#second_name').val();
var email = $('#email').val();
var number = $('#number').val();
var guests = $('#guests').val();
var message = $('#message').val();
if(first_name.length == 0){var error = true;$('#first_name_error').fadeIn(500);}else{$('#first_name_error').fadeOut(500);}
if(guests.value == Guests){var error = true;$('#guests_error').fadeIn(500);}else{$('#guests_error').fadeOut(500);}
Notice that your "guests" variable is already set to the value of the select element (using jQuery val()). There is no need to attempt to access the "value" property of the "guests" variable.
Second, the comparison you are making is to the identifier Guests, not to the string "Guests". You'll want to put quotes around that to make it a string literal.
You can see an example of this here: http://jsfiddle.net/tbuCJ/