|
||
|
API examples for newer versions Please visit the latest API Examples documentation to view API examples for newer versions of Kentico. |
The following example demonstrates how a friendship request can be created.
private bool RequestFriendship() { // First create a new user which the friendship request will be sent to UserInfo newFriend = new UserInfo(); newFriend.UserName = "MyNewFriend"; newFriend.FullName = "My new friend"; newFriend.UserGUID = Guid.NewGuid();
UserInfoProvider.SetUserInfo(newFriend);
// Create new friend object FriendInfo newFriendship = new FriendInfo();
// Set the properties newFriendship.FriendUserID = CMSContext.CurrentUser.UserID; newFriendship.FriendRequestedUserID = newFriend.UserID; newFriendship.FriendRequestedWhen = DateTime.Now; newFriendship.FriendComment = "Sample friend request comment."; newFriendship.FriendStatus = FriendshipStatusEnum.Waiting; newFriendship.FriendGUID = Guid.NewGuid();
// Save the friend FriendInfoProvider.SetFriendInfo(newFriendship);
return true; } |
The following example approves the friendship requested in the previous code example.
private bool ApproveFriendship() { // Get the users involved in the friendship UserInfo user = CMSContext.CurrentUser; UserInfo friend = UserInfoProvider.GetUserInfo("MyNewFriend");
if (friend != null) { // Get the friendship with current user FriendInfo updateFriendship = FriendInfoProvider.GetFriendInfo(user.UserID, friend.UserID); if (updateFriendship != null) { // Set its properties updateFriendship.FriendStatus = FriendshipStatusEnum.Approved; updateFriendship.FriendRejectedBy = 0; updateFriendship.FriendApprovedBy = user.UserID; updateFriendship.FriendApprovedWhen = DateTime.Now;
// Save the changes to database FriendInfoProvider.SetFriendInfo(updateFriendship);
return true; } }
return false; } |
The following example rejects the friendship requested in the first code example on this page.
private bool RejectFriendship() { // Get the users involved in the friendship UserInfo user = CMSContext.CurrentUser; UserInfo friend = UserInfoProvider.GetUserInfo("MyNewFriend");
if (friend != null) { // Get the friendship with current user FriendInfo updateFriendship = FriendInfoProvider.GetFriendInfo(user.UserID, friend.UserID);
// Set its properties updateFriendship.FriendStatus = FriendshipStatusEnum.Rejected; updateFriendship.FriendApprovedBy = 0; updateFriendship.FriendRejectedBy = user.UserID; updateFriendship.FriendRejectedWhen = DateTime.Now;
// Save the changes to database FriendInfoProvider.SetFriendInfo(updateFriendship);
return true; } else { return false; } } |
The following example demonstrates how multiple friendships can be get based on a WHERE condition and bulk updated.
private bool GetAndBulkUpdateFriends() { // Get the user UserInfo friend = UserInfoProvider.GetUserInfo("MyNewFriend");
if (friend != null) { // Prepare the parameters string where = "FriendRequestedUserID = " + friend.UserID;
// Get the data DataSet friends = FriendInfoProvider.GetFriends(where, null); if (!DataHelper.DataSourceIsEmpty(friends)) { // Loop through the individual items foreach (DataRow friendDr in friends.Tables[0].Rows) { // Create object from DataRow FriendInfo modifyFriend = new FriendInfo(friendDr);
// Update the properties modifyFriend.FriendStatus = FriendshipStatusEnum.Approved; modifyFriend.FriendRejectedBy = 0; modifyFriend.FriendApprovedBy = CMSContext.CurrentUser.UserID; modifyFriend.FriendApprovedWhen = DateTime.Now;
// Save the changes FriendInfoProvider.SetFriendInfo(modifyFriend); }
return true; } }
return false; } |
The following example shows how friendships can be selected based on a WHERE condition and bulk deleted.
private bool DeleteFriends() { // Get the user UserInfo friend = UserInfoProvider.GetUserInfo("MyNewFriend");
if (friend != null) { // Prepare the parameters string where = "FriendRequestedUserID = " + friend.UserID;
// Get all user's friendships DataSet friends = FriendInfoProvider.GetFriends(where, null); if (!DataHelper.DataSourceIsEmpty(friends)) { // Delete all the friendships foreach (DataRow friendDr in friends.Tables[0].Rows) { FriendInfo deleteFriend = new FriendInfo(friendDr);
deleteFriend.Delete(); } } else { // Change the info message apiDeleteFriends.InfoMessage = "The user 'My new friend' doesn't have any friends. The user has been deleted."; }
// Finally delete the user "My new friend" UserInfoProvider.DeleteUser(friend);
return true; }
return false; } |