การเข้าถึงและการจัดการข้อมูล Microsoft 365 ด้วย Microsoft Graph
เพิ่มประสิทธิภาพการเข้าถึงและจัดการข้อมูล Microsoft 365 ด้วย Aspose.Email Graph Client
Microsoft Graph เป็น REST API เพื่อเข้าถึงข้อมูล Microsoft 365 การทำงานของ Graph Client ใน Aspose.Email สำหรับ .NET ทำให้สามารถเข้าถึง Microsoft Graph จาก API ของเรา ในตัวอย่างต่อไปนี้ เราจะสร้างอินสแตนซ์ของ MS Graph Client พร้อมด้วยโทเคน จากนั้นเราจะตรวจสอบเมธอดหลักสำหรับการจัดการโฟลเดอร์ การอัปเดต การคัดลอกและการลบ ข้อความ เนื้อหาและไฟล์แนบของมันก็สามารถเข้าถึงหรือเปลี่ยนแปลงได้ด้วย MS Graph Client ของเรา การจัดการหมวดหมู่ กฎ สมุดโน้ตและการแทนที่เป็นฟีเจอร์ขยายของ Microsoft Graph Client โดย Aspose.Email.
การตรวจสอบสิทธิและร้องขอด้วย IGraphClient โดยใช้ MSAL ใน .NET
เพื่อโต้ตอบกับบริการ Microsoft Graph คุณจำเป็นต้องสร้าง IGraphClient อ็อบเจกต์ เมื่อตรวจสอบสิทธิสำเร็จ ไคลเอ็นต์นี้จะให้คุณทำการร้องขอบริการต่างๆ ได้ ส่วน GetClient เมธอด ซึ่งสร้าง IGraphClient, ต้องการ ITokenProvider การทำงานเป็นพารามิเตอร์แรกของมัน. ส่วนนี้ ITokenProvider เป็นผู้รับผิดชอบในการให้โทเคนการตรวจสอบสิทธิที่จำเป็น เพื่อรับโทเคน เราจะใช้ Microsoft Authentication Library (MSAL) สำหรับ .NET.
นี่คือวิธีการตั้งค่ากระบวนการตรวจสอบสิทธิ:
ขั้นตอนที่ 1: การตั้งค่าการตรวจสอบสิทธิ
ขั้นตอนต่อไปนี้จะช่วยแนะนำวิธีรับโทเคนการอนุญาต:
-
สร้างคลาส AccessParameters
กำหนดคลาส AccessParameters เพื่อเก็บข้อมูลรับรองของคุณ.
public class AccessParameters
{
public string TenantId { get; init; }
public string ClientId { get; init; }
public string ClientSecret { get; init; }
public string UserId { get; init; }
public Uri Authority => new ($"https://login.microsoftonline.com/{TenantId}");
public string ApiUrl => "https://graph.microsoft.com/.default";
}
-
เพิ่ม แพ็กเกจ MSAL.NET**.
ติดตั้ง
Microsoft.Identity.Clientแพ็กเกจ NuGet ซึ่งมีไบนารีของ MSAL.NET ที่จำเป็นสำหรับการรับรองตัวตน. -
ทำการใช้งาน Interface ITokenProvider
สร้าง
GraphTokenProviderคลาสที่ทำการใช้งาน ITokenProvider interface. คลาสนี้จะใช้ไลบรารี MSAL.NET เพื่อรับโทเคนการเข้าถึง.
using Microsoft.Identity.Client;
using Microsoft.Identity.Web;
using Aspose.Email.Clients;
public class GraphTokenProvider : ITokenProvider
{
private readonly IConfidentialClientApplication _app;
private readonly string[] _scopes;
private string? _token;
public GraphTokenProvider(AccessParameters accessParams)
{
_app = ConfidentialClientApplicationBuilder.Create(accessParams.ClientId)
.WithClientSecret(accessParams.ClientSecret)
.WithAuthority(accessParams.Authority)
.Build();
_app.AddInMemoryTokenCache();
_scopes = new[] { accessParams.ApiUrl };
}
public void Dispose()
{
throw new NotImplementedException();
}
public OAuthToken GetAccessToken()
{
return GetAccessToken(false);
}
public OAuthToken GetAccessToken(bool ignoreExistingToken)
{
if (!ignoreExistingToken && _token != null)
{
return new OAuthToken(_token);
}
_token = GetAccessTokenAsync().GetAwaiter().GetResult();
return new OAuthToken(_token);
}
private async Task<string?> GetAccessTokenAsync()
{
AuthenticationResult? result;
try
{
result = await _app.AcquireTokenForClient(_scopes)
.ExecuteAsync();
Console.WriteLine("Token acquired");
}
catch (MsalServiceException ex) when (ex.Message.Contains("AADSTS70011"))
{
Console.WriteLine("Scope provided is not supported");
result = null;
}
if (result == null) return null;
_token = result.AccessToken;
return result.AccessToken;
}
ขั้นตอนที่ 2: สร้างอินสแตนซ์ ITokenProvider
หลังจากกำหนด GraphTokenProvider คลาส, คุณสามารถสร้างอินสแตนซ์ของ AccessParameters และใช้มันเพื่อสร้างอินสแตนซ์ของ GraphTokenProvider.
var accessParams = new AccessParameters()
{
TenantId = "Your Tenant ID",
ClientId = "Your Client ID",
ClientSecret = "Your Client Secret",
UserId = "User's Object ID"
};
var tokenProvider = new GraphTokenProvider(accessParams);
ขั้นตอนที่ 3: ทำคำขอด้วย IGraphClient
สุดท้าย ใช้ GraphTokenProvider เพื่อสร้างการพิสูจน์ตัวตน IGraphClient และเริ่มทำคำขอบริการ.
using var client = GraphClient.GetClient(tokenProvider, accessParams.TenantId);
client.Resource = ResourceType.Users;
client.ResourceId = accessParams.UserId;
เมื่อทำขั้นตอนเหล่านี้เสร็จแล้ว, ของคุณ IGraphClient พร้อมใช้งานแล้วเพื่อโต้ตอบกับบริการ Microsoft Graph ด้วยคำขอที่พิสูจน์ตัวตน.
การเชื่อมต่อกับจุดสิ้นสุด GCC High
นี้ GraphClient รองรับการเชื่อมต่อกับจุดสิ้นสุด GCC High ด้วยการใช้ EndPoint คุณสมบัติ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกำหนดค่า GraphClient ให้เชื่อมต่อกับจุดสิ้นสุด GCC High เพื่อรายการโฟลเดอร์และดึงข้อความ.
client.EndPoint = "https://graph.microsoft.us";
var folders = client.ListFolders();
string folderId = folders.Find(x => x.DisplayName == "Inbox").ItemId;
var msgs = client.ListMessages(folderId);
จัดการโฟลเดอร์ด้วย IGraphClient
List Folders
โดยการเรียก ListFolders เมธอดจาก MS Graph Client ทำให้สามารถดึงรายการโฟลเดอร์ได้ แต่ละโฟลเดอร์มีชุดพารามิเตอร์เช่น DisplayName ที่สามารถอ่านได้ใน FolderInfo ประเภท.
var folders = client.ListFolders();
foreach (var folder in folders)
{
Console.WriteLine(folder.DisplayName);
}
อัปเดตโฟลเดอร์
เพื่อสร้างโฟลเดอร์ด้วย MS Graph Client ให้ใช้ CreateFolder เมธอด คุณจะได้ FolderInfo ออบเจ็กต์และความสามารถในการเข้าถึง DisplayName, ItemId, HasSubFolders และคุณสมบัติเพิ่มเติมอื่นๆ.
var folderInfo = client.CreateFolder("FolderName");
folderInfo.DisplayName = "FolderAnotherName";
client.UpdateFolder(folderInfo);
คัดลอกโฟลเดอร์
CopyFolder เมธอดนี้เป็นเมธอดหลักสำหรับคัดลอกออบเจ็กต์โฟลเดอร์ด้วย MS Graph.
var folderInfo1 = client.CreateFolder("Folder1");
var folderInfo2 = client.CreateFolder("Folder2");
// copy Folder2 to Folder1
client.CopyFolder(folderInfo1.ItemId, folderInfo2.ItemId);
ย้ายและลบโฟลเดอร์
ใช้ MoveFolder เมธอดนี้ใช้เพื่อย้ายโฟลเดอร์ โดยรับ newParentId และ itemId. ลบ เมธอดนี้ใช้เพื่อลบเมธอดตาม id.
var folderInfo1 = client.CreateFolder("Folder1");
var folderInfo2 = client.CreateFolder("Folder2");
// move Folder2 to Folder1
client.MoveFolder(folderInfo1.ItemId, folderInfo2.ItemId);
// delete Folder1
client.Delete(folderInfo1.ItemId)
จัดการข้อความด้วย IGraphClient
MS Graph Client ที่นำไปใช้ใน Aspose.Email สำหรับ .NET มีชุดเมธอดเพื่อจัดการข้อความและไฟล์แนบ:
- รายการ ข้อความ
- ดึง ข้อความ
- สร้าง ข้อความ
- ส่ง ข้อความ
- คัดลอกข้อความ ข้อความ
- ย้าย ข้อความ
- CreateAttachment
- FetchAttachment
- DeleteAttachment
- ListAttachments
แสดงรายการข้อความ
var folders = client.ListFolders();
foreach (var folder in folders)
{
if (folder.DisplayName.Equals("Inbox"))
{
// list messages in inbox
var inboxMessages = client.ListMessages(folder.ItemId);
foreach (var messageInfo in inboxMessages)
{
Console.WriteLine(messageInfo.Subject);
}
}
}
กรองข้อความตามวันที่ส่ง
นี้ OrderBy เมธอดจากคอลเลกชันของไลบรารีช่วยให้คุณดึงข้อความด้วยลำดับการจัดเรียงที่ต่างกัน (จากน้อยไปหามากและจากมากไปหาน้อย) ตามวันที่ส่ง ตัวอย่างโค้ดต่อไปนี้แสดงวิธีจัดเรียงข้อความตามวันที่ส่ง:
IGraphClient client = GraphClient.GetClient(provider, TenantId);
var builder = new GraphQueryBuilder();
// create orderby messages query 'DESC'
builder.SentDate.OrderBy(false);
var messagePageInfo = client.ListMessages(KnownFolders.Inbox, new PageInfo(10), builder.GetQuery());
var messages = messagePageInfo.Items;
builder.Clear();
// create orderby messages query 'ASC'
builder.SentDate.OrderBy(true);
messagePageInfo = client.ListMessages(KnownFolders.Inbox, new PageInfo(10), builder.GetQuery());
messages = messagePageInfo.Items;
แสดงรายการข้อความด้วยการสนับสนุนการแบ่งหน้า
API รองรับการแบ่งหน้าและการกรองข้อความเมื่อทำการรายการ ซึ่งเป็นประโยชน์อย่างยิ่งสำหรับกล่องเมลที่มีปริมาณข้อความสูง เนื่องจากช่วยประหยัดเวลาโดยดึงข้อมูลสรุปที่จำเป็นเท่านั้น.
ตัวอย่างโค้ดและขั้นตอนด้านล่างแสดงวิธีดึงข้อความจากโฟลเดอร์ Inbox โดยใช้การแบ่งหน้าและการกรอง.
- ขั้นแรก เริ่มต้น client.
- จากนั้นตั้งจำนวนรายการต่อหน้าที่จะแสดง เช่น 10.
- สร้างตัวกรองเพื่อดึงเฉพาะข้อความที่ยังไม่ได้อ่านโดยใช้ GraphQueryBuilder คลาส บิลเดอร์ .IsRead.Equals(false) ตั้งเงื่อนไขเพื่อกรองข้อความที่ยังไม่ได้อ่าน.
- เรียกใช้ ListMessages เมธอดบนอ็อบเจ็กต์ client โดยระบุโฟลเดอร์ (Inbox) และจำนวนรายการต่อหน้า (PageInfo(itemsPerPage)) เป็นพารามิเตอร์ นอกจากนี้ยังส่งอ็อบเจ็กต์ query เพื่อใช้ตัวกรองข้อความที่ยังไม่ได้อ่าน วัตถุ PageInfo ที่ส่งกลับ (pageInfo) มีข้อความที่ดึงมาสำหรับหน้าปัจจุบันในคุณสมบัติ Items.
- สร้างลูปที่ทำงานต่อจนกว่าจะถึงหน้าสุดท้าย (pageInfo.LastPage เป็น false) ข้อความที่ดึงมาจะถูกเพิ่มเข้ารายการข้อความที่มีอยู่โดยใช้ messages.AddRange(pageInfo.Items).
// reading unread messages with paging
using var client = GraphClient.GetClient(tokenProvider, config.Tenant);
// paging option
var itemsPerPage = 10;
// create unread messages filter
GraphQueryBuilder builder = new GraphQueryBuilder();
builder.IsRead.Equals(false);
var query = builder.GetQuery();
// list messages
var pageInfo = client.ListMessages(KnownFolders.Inbox, new PageInfo(itemsPerPage), query);
var messages = pageInfo.Items;
while (!pageInfo.LastPage)
{
pageInfo = client.ListMessages(KnownFolders.Inbox, pageInfo.NextPage, query);
messages.AddRange(pageInfo.Items);
}
// set messages state as read
foreach (var message in messages)
{
client.SetRead(message.ItemId);
}
ดึงข้อความ
var folders = client.ListFolders();
foreach (var folder in folders)
{
if (folder.DisplayName.Equals("Inbox"))
{
// list messages in inbox
var inboxMessages = client.ListMessages(folder.ItemId);
if (inboxMessages.Count > 0)
{
// fetch the first message in inbox
var msg = client.FetchMessage(inboxMessages[0].ItemId);
Console.WriteLine(msg.BodyHtml);
}
}
}
สร้างข้อความ
var msg = new MapiMessage(OutlookMessageFormat.Unicode)
{
Subject = "My message",
Body = "Hi, it is my message"
};
msg.Recipients.Add("sam@to.com", "Sam", MapiRecipientType.MAPI_TO);
// create message in inbox
client.CreateMessage(KnownFolders.Inbox, msg);
ส่งข้อความ
// prepare the message
var msg = new MapiMessage(OutlookMessageFormat.Unicode)
{
Subject = "My message",
Body = "Hi, it is my message"
};
msg.Recipients.Add("sam@to.com", "Sam", MapiRecipientType.MAPI_TO);
msg.SetProperty(KnownPropertyList.SenderName, "John");
msg.SetProperty(KnownPropertyList.SentRepresentingEmailAddress, "John@from.com");
// send message
client.Send(msg);
ส่งข้อความฉบับร่าง
// prepare the message
var msg = new MapiMessage(OutlookMessageFormat.Unicode)
{
Subject = "My message",
Body = "Hi, it is my message"
};
msg.Recipients.Add("sam@to.com", "Sam", MapiRecipientType.MAPI_TO);
msg.SetProperty(KnownPropertyList.SenderName, "John");
msg.SetProperty(KnownPropertyList.SentRepresentingEmailAddress, "John@from.com");
// add message to Draft folder
var draftMessage = client.CreateMessage(KnownFolders.Drafts, msg);
// send a draft message
client.Send(draftMessage.ItemId);
ส่งข้อความ EML
การสร้างและส่งอีเมลเป็นเรื่องง่ายโดยใช้วัตถุ MailMessage ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างและส่งข้อความอีเมลโดยใช้ Graph API:
// prepare the message
var eml = new MailMessage
{
From = "from@domain.com",
To = "to1@domain.com, to2@domain.com",
Subject = "New message",
HtmlBody = "<html><body>This is the HTML body</body></html>"
};
// send the message
graphClient.Send(eml);
graphClient.Create(KnownFolders.Inbox, eml);
คัดลอกข้อความ
// copy message to Inbox folder
var copiedMsg = client.CopyMessage(KnownFolders.Inbox, msg.ItemId);
ย้ายข้อความ
// move message to Inbox folder
var movedMsg = client.MoveMessage(KnownFolders.Inbox, msg.ItemId);
จัดการไฟล์แนบ
// create an attachment
var attachment = new MapiAttachment();
attachment.SetProperty(KnownPropertyList.DisplayName, "My Attachment");
attachment.SetProperty(KnownPropertyList.AttachDataBinary, new byte[1024]);
// add an attachment to message
var createdAttachment = client.CreateAttachment(messageInfo.ItemId, attachment);
// fetch a message attachment
var fetchedAttachment = client.FetchAttachment(createdAttachment.ItemId);
// delete a message attachment
client.DeleteAttachment(createdAttachment.ItemId);
// list the message attachments
var attachments = client.ListAttachments(messageInfo.ItemId);
จัดการรายการ Outlook ด้วย Graph Client
จัดการเหตุการณ์ปฏิทิน
Aspose.Email มี API เพื่อเข้าถึง, จัดการ, และโต้ตอบกับเหตุการณ์ปฏิทิน สำหรับวัตถุประสงค์เหล่านี้ มีเมธอดต่อไปนี้ใน IGraphClient อินเทอร์เฟซ:
- ListCalendars() - ดึงคอลเลกชันของข้อมูลปฏิทิน
- ListCalendarItems(string id) - ดึงคอลเลกชันของรายการปฏิทินที่เชื่อมโยงกับ ID ปฏิทินที่ระบุ
- FetchCalendarItem(string id) - ดึงรายการปฏิทินที่ระบุโดยใช้ ID ที่ให้
- CreateCalendarItem(string calId, MapiCalendar mapiCalendar) - สร้างรายการปฏิทินใหม่ในปฏิทินที่ระบุ
- UpdateCalendarItem(MapiCalendar mapiCalendar) - อัปเดตรายการปฏิทินที่มีอยู่
- UpdateCalendarItem(MapiCalendar mapiCalendar, UpdateSettings updateSettings) - อัปเดตรายการปฏิทินที่มีอยู่ด้วยการตั้งค่าอัปเดตที่ระบุ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีโต้ตอบกับเหตุการณ์ปฏิทินในไคลเอนต์ Microsoft Graph API โดยใช้เมธอดที่ให้โดย Aspose.Email:
// List Calendars
CalendarInfoCollection calendars = graphClient.ListCalendars();
// List Calendar Items
MapiCalendarCollection calendarItems = graphClient.ListCalendarItems("calendarId");
// Fetch Calendar Item
MapiCalendar calendarItem = graphClient.FetchCalendarItem("calendarItemId");
// Create Calendar Item
MapiCalendar newCalendarItem = new MapiCalendar(
location: "Conference Room",
summary: "Team Meeting",
description: "Discuss project status and updates.",
startDate: startDate,
endDate: endDate
);
MapiCalendar createdCalendarItem = graphClient.CreateCalendarItem("calendarId", newCalendarItem);
// Update Calendar Item
createdCalendarItem.Location = "Zoom Meeting";
MapiCalendar updatedCalendarItem = graphClient.UpdateCalendarItem(createdCalendarItem);
จัดการหมวดหมู่
เพื่อจัดการหมวดหมู่ด้วย MS Graph ผ่าน Aspose.Email สำหรับ .NET ให้ใช้เมธอดต่อไปนี้:
// create a custom category with Orange color
var category = client.CreateCategory("My custom category", CategoryPreset.Preset1);
// fetch a category
var fetchedCategory = client.FetchCategory(category.Id);
// update category (change color to brown)
fetchedCategory.Preset = CategoryPreset.Preset2;
var updatedCategory = client.UpdateCategory(fetchedCategory);
// list available categories
var categories = client.ListCategories();
foreach (var cat in categories)
{
Console.WriteLine(cat.DisplayName);
}
// delete a category
client.Delete(fetchedCategory.Id);
จัดการผู้ติดต่อ
Aspose.Email มี API เพื่อเข้าถึง, จัดการ, และโต้ตอบกับรายการผู้ติดต่อ สำหรับวัตถุประสงค์เหล่านี้ มีเมธอดต่อไปนี้ใน IGraphClient อินเทอร์เฟซ:
- ListContacts(string id) - ดึงคอลlection ของผู้ติดต่อ MAPI ที่เชื่อมโยงกับ ID โฟลเดอร์ที่ระบุ
- FetchContact(string id) - ดึงข้อมูลผู้ติดต่อที่ระบุโดยใช้ ID ของรายการที่ให้
- CreateContact(string folderId, MapiContact contact) - สร้างรายชื่อผู้ติดต่อใหม่ในโฟลเดอร์ที่ระบุ
- UpdateContact(MapiContact contact) - อัปเดตรายชื่อผู้ติดต่อที่มีอยู่
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโต้ตอบกับรายชื่อผู้ติดต่อในไคลเอนต์ Microsoft Graph API โดยใช้เมธอดที่ให้โดย Aspose.Email:
// List Contacts
MapiContactCollection contacts = graphClient.ListContacts("contactFolderId");
// Fetch Contact
MapiContact contact = graphClient.FetchContact("contactId");
// Create Contact
MapiContact newContact = new MapiContact("Jane Smith", "jane.smith@example.com", "XYZ Corporation", "777-888-999");
MapiContact createdContact = graphClient.CreateContact("contactFolderId", newContact);
// Update Contact
createdContact.Telephones.PrimaryTelephoneNumber = "888-888-999";
MapiContact updatedContact = graphClient.UpdateContact(createdContact);
จัดการการแทนที่
เพื่อจัดการการแทนที่ด้วย MS Graph ผ่าน Aspose.Email สำหรับ .NET ให้ใช้เมธอดต่อไปนี้:
// Create an user's override
var userOverride = client.CreateOrUpdateOverride
(new MailAddress("JohnBrown@someorg.com", "JohnBrown"), ClassificationType.Focused);
// list the overrides
var overrides = client.ListOverrides();
// update override
userOverride.Sender.DisplayName = "John Brown";
var updatedOverride = client.UpdateOverride(userOverride);
// delete override
client.Delete(updatedOverride.Id);
จัดการกฎ
เพื่อจัดการกฎด้วย MS Graph ผ่าน Aspose.Email สำหรับ .NET ให้ใช้เมธอดต่อไปนี้:
// Create a rule
var rule = PrepareRule("user@someorg.com", "User");
var createdRule = client.CreateRule(rule);
// List all rules defined for Inbox
var rules = client.ListRules();
// Fetch a rule
var fetchedRule = client.FetchRule(createdRule.RuleId);
// Update a rule
fetchedRule.DisplayName = "Renamed rule";
fetchedRule.IsEnabled = false;
var updatedRule = client.UpdateRule(createdRule);
// Delete a rule
client.Delete(updatedRule.RuleId);
InboxRule PrepareRule(string email, string displayName)
{
var rule = new InboxRule()
{
DisplayName = "My rule",
Priority = 1,
IsEnabled = true,
Conditions = new RulePredicates(),
Actions = new RuleActions()
};
rule.Conditions.ContainsSenderStrings = new StringCollection { displayName };
rule.Actions.ForwardToRecipients = new MailAddressCollection
{ new MailAddress(email, displayName, true) };
rule.Actions.StopProcessingRules = true;
return rule;
}
จัดการสมุดโน้ต
เพื่อจัดการสมุดโน้ตด้วย MS Graph ผ่าน Aspose.Email สำหรับ .NET ให้ใช้เมธอดต่อไปนี้:
// create a OneNote notebook
var newNotebook = new Notebook()
{
DisplayName = "My Notebook"
};
var createdNotebook = client.CreateNotebook(newNotebook);
// fetch a notebook
var fetchedNotebook = client.FetchNotebook(createdNotebook.Id);
// list the notebooks
var notebooks = client.ListNotebooks();
การจัดการงานใน Microsoft Graph
Aspose.Email ให้ API แก่ผู้พัฒนาเพื่อเข้าถึง, จัดการ, และทำงานกับงานและรายการงานของผู้ใช้โดยใช้เมธอดต่อไปนี้ของ IGraphClient อินเทอร์เฟซ:
- ListTaskLists() - ดึงคอลเลกชันของข้อมูลรายการงาน.
- GetTaskList(string id) - ดึงรายการงานเฉพาะตาม ID ที่ให้มา.
- DeleteTaskList(string id) - ลบรายการงานที่ระบุ -ListTasks(string id) - ดึงชุดของภารกิจที่เชื่อมโยงกับ ID ของรายการภารกิจที่ระบุ.
- FetchTask(string id) - ดึงภารกิจเฉพาะตาม ID ที่ให้มา.
- CreateTask(MapiTask task, string taskListUri) - สร้างภารกิจใหม่ในรายการภารกิจที่ระบุ.
- UpdateTask(MapiTask task) - อัปเดตภารกิจที่มีอยู่ด้วยข้อมูลที่ให้มา.
- UpdateTask(MapiTask task, UpdateSettings updateSettings) - อัปเดตภารกิจที่มีอยู่ด้วยการตั้งค่าการอัปเดตที่ระบุ.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีจัดการรายการงาน:
// List Task Lists
var taskLists = graphClient.ListTaskLists();
foreach (var tList in taskLists)
{
Console.WriteLine($"Task List: {tList.DisplayName}");
}
// Get Task List
var taskList = graphClient.GetTaskList("taskListId");
// Delete Task List
graphClient.DeleteTaskList("taskListId");
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีจัดการงาน:
// List Tasks in a Task List
MapiTaskCollection tasks = graphClient.ListTasks("taskListId");
// Fetch Task
MapiTask task = graphClient.FetchTask("taskId");
// Create Task
var newTask = new MapiTask
{
Subject = "New Task",
DueDate = new DateTime(2023, 12, 31),
Status = MapiTaskStatus.NotStarted
};
MapiTask createdTask = graphClient.CreateTask(newTask, "taskListUri");
// Update Task
createdTask.Subject = "Updated Task Subject";
MapiTask updatedTask = graphClient.UpdateTask(createdTask);
// Update Task with UpdateSettings
var updateSettings = new UpdateSettings { SkipAttachments = true };
MapiTask updatedTaskWithSettings = graphClient.UpdateTask(createdTask, updateSettings);