本文整理汇总了C#中GroupMember.SaveAttributeValues方法的典型用法代码示例。如果您正苦于以下问题:C# GroupMember.SaveAttributeValues方法的具体用法?C# GroupMember.SaveAttributeValues怎么用?C# GroupMember.SaveAttributeValues使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GroupMember
的用法示例。
在下文中一共展示了GroupMember.SaveAttributeValues方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnMoveRegistration_Click
protected void btnMoveRegistration_Click( object sender, EventArgs e )
{
// set the new registration id
using ( var rockContext = new RockContext() )
{
var registrationService = new RegistrationService( rockContext );
var groupMemberService = new GroupMemberService( rockContext );
var registration = registrationService.Get( Registration.Id );
registration.RegistrationInstanceId = ddlNewRegistrationInstance.SelectedValue.AsInteger();
// Move registrants to new group
int? groupId = ddlMoveGroup.SelectedValueAsInt();
if ( groupId.HasValue )
{
registration.GroupId = groupId;
rockContext.SaveChanges();
var group = new GroupService( rockContext ).Get( groupId.Value );
if ( group != null )
{
int? groupRoleId = null;
var template = registration.RegistrationInstance.RegistrationTemplate;
if ( group.GroupTypeId == template.GroupTypeId && template.GroupMemberRoleId.HasValue )
{
groupRoleId = template.GroupMemberRoleId.Value;
}
if ( !groupRoleId.HasValue )
{
groupRoleId = group.GroupType.DefaultGroupRoleId;
}
if ( !groupRoleId.HasValue )
{
groupRoleId = group.GroupType.Roles.OrderBy( r => r.Order ).Select( r => r.Id ).FirstOrDefault();
}
if ( groupRoleId.HasValue )
{
foreach ( var registrant in registration.Registrants.Where( r => r.PersonAlias != null ) )
{
var newGroupMembers = groupMemberService.GetByGroupIdAndPersonId( groupId.Value, registrant.PersonAlias.PersonId );
if ( !newGroupMembers.Any() )
{
// Get any existing group member attribute values
var existingAttributeValues = new Dictionary<string, string>();
if ( registrant.GroupMemberId.HasValue )
{
var existingGroupMember = groupMemberService.Get( registrant.GroupMemberId.Value );
if ( existingGroupMember != null )
{
existingGroupMember.LoadAttributes( rockContext );
foreach ( var attributeValue in existingGroupMember.AttributeValues )
{
existingAttributeValues.Add( attributeValue.Key, attributeValue.Value.Value );
}
}
registrant.GroupMember = null;
groupMemberService.Delete( existingGroupMember );
}
var newGroupMember = new GroupMember();
groupMemberService.Add( newGroupMember );
newGroupMember.Group = group;
newGroupMember.PersonId = registrant.PersonAlias.PersonId;
newGroupMember.GroupRoleId = groupRoleId.Value;
rockContext.SaveChanges();
newGroupMember = groupMemberService.Get( newGroupMember.Id );
newGroupMember.LoadAttributes();
foreach( var attr in newGroupMember.Attributes )
{
if ( existingAttributeValues.ContainsKey( attr.Key ) )
{
newGroupMember.SetAttributeValue( attr.Key, existingAttributeValues[attr.Key] );
}
}
newGroupMember.SaveAttributeValues( rockContext );
registrant.GroupMember = newGroupMember;
rockContext.SaveChanges();
}
}
}
}
}
// Reload registration
Registration = GetRegistration( Registration.Id );
lWizardInstanceName.Text = Registration.RegistrationInstance.Name;
ShowReadonlyDetails( Registration );
}
mdMoveRegistration.Hide();
}
示例2: btnMoveGroupMember_Click
/// <summary>
/// Handles the Click event of the btnMoveGroupMember control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void btnMoveGroupMember_Click( object sender, EventArgs e )
{
var rockContext = new RockContext();
var groupMemberService = new GroupMemberService( rockContext );
var groupMember = groupMemberService.Get( hfGroupMemberId.Value.AsInteger() );
groupMember.LoadAttributes();
int destGroupId = gpMoveGroupMember.SelectedValue.AsInteger();
var destGroup = new GroupService( rockContext ).Get( destGroupId );
var destGroupMember = groupMemberService.Queryable().Where( a =>
a.GroupId == destGroupId
&& a.PersonId == groupMember.PersonId
&& a.GroupRoleId == grpMoveGroupMember.GroupRoleId ).FirstOrDefault();
if ( destGroupMember != null )
{
nbMoveGroupMemberWarning.Visible = true;
nbMoveGroupMemberWarning.Text = string.Format( "{0} is already in {1}", groupMember.Person, destGroupMember.Group );
return;
}
if ( !grpMoveGroupMember.GroupRoleId.HasValue )
{
nbMoveGroupMemberWarning.Visible = true;
nbMoveGroupMemberWarning.Text = string.Format( "Please select a Group Role" );
return;
}
string canDeleteWarning;
if ( !groupMemberService.CanDelete( groupMember, out canDeleteWarning ) )
{
nbMoveGroupMemberWarning.Visible = true;
nbMoveGroupMemberWarning.Text = string.Format( "Unable to remove {0} from {1}: {2}", groupMember.Person, groupMember.Group, canDeleteWarning );
return;
}
destGroupMember = new GroupMember();
destGroupMember.GroupId = destGroupId;
destGroupMember.GroupRoleId = grpMoveGroupMember.GroupRoleId.Value;
destGroupMember.PersonId = groupMember.PersonId;
destGroupMember.LoadAttributes();
foreach ( var attribute in groupMember.Attributes )
{
if ( destGroupMember.Attributes.Any( a => a.Key == attribute.Key && a.Value.FieldTypeId == attribute.Value.FieldTypeId ) )
{
destGroupMember.SetAttributeValue( attribute.Key, groupMember.GetAttributeValue( attribute.Key ) );
}
}
rockContext.WrapTransaction( () =>
{
groupMemberService.Add( destGroupMember );
rockContext.SaveChanges();
destGroupMember.SaveAttributeValues( rockContext );
// move any Note records that were associated with the old groupMember to the new groupMember record
if ( cbMoveGroupMemberMoveNotes.Checked )
{
destGroupMember.Note = groupMember.Note;
int groupMemberEntityTypeId = EntityTypeCache.GetId<Rock.Model.GroupMember>().Value;
var noteService = new NoteService( rockContext );
var groupMemberNotes = noteService.Queryable().Where( a => a.NoteType.EntityTypeId == groupMemberEntityTypeId && a.EntityId == groupMember.Id );
foreach ( var note in groupMemberNotes )
{
note.EntityId = destGroupMember.Id;
}
rockContext.SaveChanges();
}
groupMemberService.Delete( groupMember );
rockContext.SaveChanges();
destGroupMember.CalculateRequirements( rockContext, true );
} );
var queryString = new Dictionary<string, string>();
queryString.Add( "GroupMemberId", destGroupMember.Id.ToString() );
this.NavigateToPage( this.RockPage.Guid, queryString );
}