Adding a new user to CRM 4.0 using sdk - dynamics-crm

Does anyone have sample code to add a new user to CRM 4.0 using sdk?

I have code that creates users for us based on users in another system so I can't exactly paste it all here - most of it wouldn't make sense to you - but this is the core of it:
[In VB sorry :-) - also when posting VB here I find I need to use '//' to indicate a comment to make the formatting correct]
Public Sub CreateNewUser()
Dim s as mscrm.CrmService = GetMyService()
Dim newUser as New mscrm.systemuser()
With newUser
.domainname = "domain\user"
.firstname = "Stan"
.lastname = "Molda"
//set anything else you want here
End With
Dim userGuid as guid = s.Create(newUser)
//Next we need to assign the user a role
AssignRole(userGuid)
//Finally we need to assign them to the correct Time Zone
SetUserTimeZone(userGuid)
End Sub
Public Sub AssignRole(g as Guid)
Dim s as mscrm.CrmService = GetMyService()
Dim req As New mscrm.AssignUserRolesRoleRequest()
req.UserId = g
req.RoleIds = New Guid() {GetTheGuidForMyPrimaryRole()}
s.Execute(req)
End Sub
Public Sub SetUserTimeZone(g as Guid)
Dim s as mscrm.CrmService = GetMyService()
Dim r As New mscrm4.RetrieveUserSettingsSystemUserRequest()
r.ColumnSet = New mscrm3.AllColumns()
r.EntityId = New Guid(g)
Dim resp As mscrm.RetrieveUserSettingsSystemUserResponse = CType(s.Execute(r), mscrm.RetrieveUserSettingsSystemUserResponse)
Dim settings As mscrm.usersettings = CType(resp.BusinessEntity, mscrm.usersettings)
settings.timezonecode = New mscrm.CrmNumber
settings.timezonecode.Value = OUR_TIME_ZONE_CONSTANT
Dim update As New mscrm.UpdateUserSettingsSystemUserRequest()
update.Settings = settings
update.UserId = g
s.Execute(update)
End Sub

For C#, take a look at my question, Dynamics CRM: Create users with specific GUIDs, which does exactly what you want (but not exactly what I want :-P).

Related

How to translate a part using CATscript in CATIA?

I working with CATscript in CATIA to create Macros. I am trying to create a CATscript to translate a feature in CATIA.
When I run the CATscript I Should select the feature that should be translated and and the feature will be translated.
But I am getting an runtime error Type mismatch:'part1.CreateReferenceFromObject'
I could not find the solution for this problem.
Looking forward for your help.
Thanks in Advance.
Sub CATMain()
Set partDocument1 = CATIA.ActiveDocument
Set part1 = partDocument1.Part
Set hybridShapeFactory1 = part1.HybridShapeFactory
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirectionByCoord(1.000000, 0.000000, 0.000000)
Set hybridShapeTranslate1 = hybridShapeFactory1.AddNewEmptyTranslate()
Set UserSel = partDocument1.Selection
Dim type1(0)
type1(0) = "HybridShape"
'--------------------------------------
'Dim input As Object
input = UserSel.SelectElement2(type1, "select input.", False)
Set reference1 = part1.CreateReferenceFromObject(input)
hybridShapeTranslate1.ElemToTranslate = reference1
hybridShapeTranslate1.Direction = hybridShapeDirection1
hybridShapeTranslate1.DistanceValue = 1.000000
Set hybridBody2 = hybridBodies1.Item("Geometrical Set.3")
hybridBody2.AppendHybridShape hybridShapeTranslate1
part1.InWorkObject = hybridShapeTranslate1
part1.Update
End Sub
your problem is that you are trying to create a reference from a Selection object.
input = UserSel.SelectElement2(type1, "select input.", False)
This returns the type Selection. You can dig into the input and get the actual object that you select.
try:
Dim myReference as Reference
Dim myExpectedObject as HybridShape 'or use variant
Set mySelectedObject = input.Item2(1).Value 'this will grab the first item from the selection collection
set myReference = part1.CreateReferenceFromObject(mySelectedObject)
'continue the rest of your code
Also, you should always clear the selection before you use a user selection as a good habit.
UserSel.Clear 'call this before you call a SelectElement selection function

get contact information for a particular email address

I am trying to write a script that would
go through a list of email addresses from my domain
for each of these emails addressess get the display name and job title
output the email, display name, job title to a csv file
I am sure this is fairly simply, the only problem that I have here is that I have no idea how to access the contact card of a contact.
How do I pass the strAddress variable to a olContactItem object?
EDIT:
To improve the question - I don't know how can I view the contact of the existing user#mydomain.com from a list of email addresses in a csv file (not added to my contacts list or anything)
The code I have so far:
Const olContactItem = 2
strEmail = "user#mydomain.com"
set fso = CreateObject("Scripting.FileSystemObject")
set appOutlook = CreateObject("Outlook.Application")
Set MyItem = appOutlook.CreateItem(olContactItem)
With MyItem
.Email1Address = strEmail
.jobTitle = strJobTitleVar
End With
I need to open the addess book page of that person, extract the values of Job Title and Display Name to relevant variables. However, I get stuck, because I get to a point where I am rather adding a new contact than viewing the existing person's info.
Is this more clear? How can I search through the address book for a particular person's info?
Answering my own question - this seems to be doing everything I need:
strInputEmail = objInputFile.ReadLine
set appOutlook = CreateObject("Outlook.Application")
set objNameSpace = appOutlook.GetNameSpace("MAPI")
set objContacts = objNameSpace.GetDefaultFolder(olFolderContacts)
Set objContact = objContacts.Items.Find("[IMAddress] = """ & strInputEmail & """")
strFullName = objContact.FullName
strJobTitle = objContact.JobTitle
strBusinessAddress = objContact.BusinessAddress
msgBox strFullName & strJobTitle & strBusinessAddress
It reads the inputfile, gets the email address as a variable, finds the person, gets the particular info as variables.
Thanks for help anyway!
Try something along the following lines. To see what the other properties and their DASL names are, look at the live GAL objects using OutlookSpy (I am its author): either click IAddrBook button and drill down the container hierarchy or click IMAPISession | QueryIdentity to see the current user or (if you have a message with a particular recipient) click IMessage, go to the GetRecipientTable table, double on the recipient.
Once you have the IMailUser window, select the property that you need and look at the DASL edit box.
Const olContactItem = 2
strEmail = "user#mydomain.com"
set fso = CreateObject("Scripting.FileSystemObject")
set appOutlook = CreateObject("Outlook.Application")
set NS = appOutlook.GetNamespace("MAPI")
NS.Logon
set Recip = NS.CreateRecipient(strEmail)
Recip.Resolve
set AE = Recip.AddressEntry
Set MyItem = appOutlook.CreateItem(olContactItem)
With MyItem
.Email1Address = strEmail
'read PR_TITLE property
.jobTitle = AE.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A17001F")
End With

How to add data to a specific column in an existing excel file using VBScript

I'm currently doing automation testing and need to write a dynamic value to an existing excel document in a specific column, this is what I have so far. Forgive I'm a novice
Sub WriteTRNtoExcelDoc
Dim fileName, sheetName
fname = "<Path_To_The_File>"
sheetName = "Sheet1"
Set app = Sys.OleObject("Excel.Application")
Set book = app.Workbooks.Open(fname)
Set sheet = book.Sheets(sheetName)
' What do I do next to add a value to a specific column or cell in this
' spreadsheet?
End Sub
Thanks in advance!
You create an Excel instance in a VBScript with
CreateObject("Excel.Application")
An already running Excel instance can be grabbed with
GetObject(, "Excel.Application")
In a worksheet you can access cells by using the Cells property:
Set app = CreateObject("Excel.Application")
app.Visible = True
Set book = app.Workbooks.Open(fname)
Set sheet = book.Sheets(sheetName)
sheet.Cells(2,3).Value = "foo"
Edit: If you need to find the first empty cell in a given column, you can use something like this:
row = 1
Do Until IsEmpty(sheets.Cells(row, 3).Value)
row = row + 1
Loop
sheet.Cells(row, 3).Value = RemPropValue

deleting a record in linq to sql (vb.net) what is wrong with my code?

I am getting the correct Employee Id in the VarEmpID variable. When I click on delete
It is giving me
Unable to cast object of type 'System.Data.Linq.DataQuery`1[my name space]' to type 'namespace'.
enter code here
Protected Sub radGrid1_DeleteCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles radGrid1.DeleteCommand
Dim VarEmpId As String = (CType(e.Item, GridDataItem)).OwnerTableView.DataKeyValues(e.Item.ItemIndex)("EmpId").ToString()
Using dc1 As New EmployeesDataClassesDataContext()
Dim EmployeeEntry = (From p In dc1.Employees
Where (p.EmpId = VarEmpId)
Select p)
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
dc1.SubmitChanges()
Dim queryResults = (From queryItem In EmployeeEntry Select queryItem).ToList()
If queryResults.Any Then
radGrid1.DataSource = queryResults
radGrid1.DataBind()
End If
End Using
End Sub
dc1.Employees.DeleteOnSubmit(EmployeeEntry)
That method expects an Employee instance. Instead, you passed in an employee query.
Dim EmployeeEntry = ( query )
This is a query, not an entry. Consider calling Enumerable.First to get the first result of the query, and then deleting that.
Modified added Dim EmployeeEntry = (From p In dc1.Employees Where (p.EmpId = VarEmpId) Select p).singleorDefault() After that commented out the queryresults part and binded data again it solved my problem. – SmilingLily

Linq does not group in VB.Net

For educational purposes I tried to convert the following Linq expression from the book "Linq in action" into VB.net
Original C#
var list =
from book in SampleData.Books
group book by new { book.Publisher.Name, book.Subject }
into grouping
select new {
Publisher = grouping.Key.Publisher,
Subject = grouping.Key.Subject,
Book = grouping
};
My attempt:
Dim list = _books.GroupBy(Function(book) New With {.Publisher = book.Publisher.Name,
book.Subject}).
Select(Function(grouping) New With {.Publisher = grouping.Key.Publisher,
.Subject = grouping.Key.Subject,
.Books = grouping})
For Each item In list
Console.WriteLine("Publisher:" & item.Publisher & ", Subject:" & item.Subject)
For Each Book In item.Books
Console.WriteLine(" " & Book.Title)
Next
Next
This leads to the following output:
Publisher:FunBooks, Subject:Fun
Funny Stories
Publisher:Joe Publishing, Subject:Work
LINQ rules
Publisher:Joe Publishing, Subject:Work
C# on rails
Publisher:Joe Publishing, Subject:Fun
All your base are belong to us
Publisher:FunBooks, Subject:Fun
Bonjour mon Amour
I expected, that the books "LINQ rules" and "C# on rails" are grouped as well as the books "Funny Stories" and "Bonjour mon Amour" because they have the same Publisher and Subject.
My anonymous key consists a new object of two simple strings.
I already tried to search in SO, but other (or) answers do not solve my problem. Even some code translators like telerik or carlosag are no help in this case.
This is the problem:
GroupBy(Function(book) New With {.Publisher = book.Publisher.Name,
book.Subject})
That's not equivalent to the C# version, because unfortunately VB uses mutable properties in anonymous types by default, and mutable properties aren't considered as part of the hash code or equality operations. You need to make both properties "Key" properties:
GroupBy(Function(book) New With {Key .Publisher = book.Publisher.Name,
Key book.Subject})
Then it should work fine. You can read more about anonymous types in VB on MSDN.
While I applaud your efforts to translate the samples, we actually have all of the samples for LINQ in Action in C# and VB available for download from the Manning Site: http://www.manning.com/marguerie/. Also, we have added samples to the LinqPad samples to make it easy to try the samples and save your changes. See http://www.thinqlinq.com/Default/LINQ-In-Action-Samples-available-in-LINQPad.aspx for instructions on how to access that.
It appears that you are working on example 5.06b. Updating it slightly, our VB translation is:
Dim query = _
From book In SampleData.Books _
Group book.Title By book.Publisher, book.Subject Into grouping = Group _
Select _
Publisher = Publisher.Name, _
Subject = Subject.Name, _
Titles = grouping
If you want to use the Lambda syntax, you do need to specify the Key as #johnskeet indicated:
Dim list = SampleData.Books.GroupBy(Function(book) New With {
Key .Publisher = book.Publisher.Name,
Key .Subject = book.Subject}).
Select(Function(grouping) New With {
.Publisher = grouping.Key.Publisher,
.Subject = grouping.Key.Subject,
.Books = grouping})

Resources