Makale
01/20/2022
Okumak için 3 dakika
9 katılımcı
Bu makalede
Namespace: microsoft.graph
Add a contact to the root Contacts folder or to the contacts endpoint of another contact folder.
Permissions
One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions .
Permission type
Permissions (from least to most privileged)
Delegated (work or school account)
Contacts.ReadWrite
Delegated (personal Microsoft account)
Contacts.ReadWrite
Application
Contacts.ReadWrite
HTTP request
POST /me/contacts
POST /users/{id | userPrincipalName}/contacts
POST /me/contactFolders/{contactFolderId}/contacts
POST /users/{id | userPrincipalName}/contactFolders/{contactFolderId}/contacts
Header
Value
Authorization
Bearer {token}. Required.
Content-Type
application/json
Request body
In the request body, supply a JSON representation of contact object.
Response
If successful, this method returns 201 Created
response code and contact object in the response body.
Example
Request
The following is an example of the request.
POST https://graph.microsoft.com/v1.0/me/contacts
Content-type: application/json
{
"givenName": "Pavel",
"surname": "Bansky",
"emailAddresses": [
{
"address": "pavelb@fabrikam.onmicrosoft.com",
"name": "Pavel Bansky"
}
],
"businessPhones": [
"+1 732 555 0102"
]
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var contact = new Contact
{
GivenName = "Pavel",
Surname = "Bansky",
EmailAddresses = new List<EmailAddress>()
{
new EmailAddress
{
Address = "pavelb@fabrikam.onmicrosoft.com",
Name = "Pavel Bansky"
}
},
BusinessPhones = new List<String>()
{
"+1 732 555 0102"
}
};
await graphClient.Me.Contacts
.Request()
.AddAsync(contact);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
const options = {
authProvider,
};
const client = Client.init(options);
const contact = {
givenName: 'Pavel',
surname: 'Bansky',
emailAddresses: [
{
address: 'pavelb@fabrikam.onmicrosoft.com',
name: 'Pavel Bansky'
}
],
businessPhones: [
'+1 732 555 0102'
]
};
await client.api('/me/contacts')
.post(contact);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/contacts"]]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphContact *contact = [[MSGraphContact alloc] init];
[contact setGivenName:@"Pavel"];
[contact setSurname:@"Bansky"];
NSMutableArray *emailAddressesList = [[NSMutableArray alloc] init];
MSGraphEmailAddress *emailAddresses = [[MSGraphEmailAddress alloc] init];
[emailAddresses setAddress:@"pavelb@fabrikam.onmicrosoft.com"];
[emailAddresses setName:@"Pavel Bansky"];
[emailAddressesList addObject: emailAddresses];
[contact setEmailAddresses:emailAddressesList];
NSMutableArray *businessPhonesList = [[NSMutableArray alloc] init];
[businessPhonesList addObject: @"+1 732 555 0102"];
[contact setBusinessPhones:businessPhonesList];
NSError *error;
NSData *contactData = [contact getSerializedDataWithError:&error];
[urlRequest setHTTPBody:contactData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Contact contact = new Contact();
contact.givenName = "Pavel";
contact.surname = "Bansky";
LinkedList<EmailAddress> emailAddressesList = new LinkedList<EmailAddress>();
EmailAddress emailAddresses = new EmailAddress();
emailAddresses.address = "pavelb@fabrikam.onmicrosoft.com";
emailAddresses.name = "Pavel Bansky";
emailAddressesList.add(emailAddresses);
contact.emailAddresses = emailAddressesList;
LinkedList<String> businessPhonesList = new LinkedList<String>();
businessPhonesList.add("+1 732 555 0102");
contact.businessPhones = businessPhonesList;
graphClient.me().contacts()
.buildRequest()
.post(contact);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
requestBody := msgraphsdk.NewContact()
givenName := "Pavel"
requestBody.SetGivenName(&givenName)
surname := "Bansky"
requestBody.SetSurname(&surname)
requestBody.SetEmailAddresses( []EmailAddress {
msgraphsdk.NewEmailAddress(),
address := "pavelb@fabrikam.onmicrosoft.com"
SetAddress(&address)
name := "Pavel Bansky"
SetName(&name)
}
requestBody.SetBusinessPhones( []String {
"+1 732 555 0102",
}
result, err := graphClient.Me().Contacts().Post(requestBody)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
Import-Module Microsoft.Graph.PersonalContacts
$params = @{
GivenName = "Pavel"
Surname = "Bansky"
EmailAddresses = @(
@{
Address = "pavelb@fabrikam.onmicrosoft.com"
Name = "Pavel Bansky"
}
)
BusinessPhones = @(
"+1 732 555 0102"
)
}
# A UPN can also be used as -UserId.
New-MgUserContact -UserId $userId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
Response
The following is an example of the response. Note: The response object shown here might be shortened for readability.
HTTP/1.1 201 Created
Content-type: application/json
{
"id": "id-value",
"createdDateTime": "2015-11-09T02:14:32Z",
"lastModifiedDateTime": "2015-11-09T02:14:32Z",
"displayName": "Pavel Bansky"
}