using Kentico cms Membership Validity not able assign

anand kamane asked on July 26, 2018 15:00

I am developing shopping website using kentico cms.In that I assigned membership expiry using admin panel (Product=>Membership=>My Product=>in membership section=> membership validity). But I want to change this property value at runtime using code behind(C#).I tried to do following thing but it’s not reflecting in database. I want to change the membership expiry year based on condition. I set this property by default is 1 year using admin panel. “currentUser.MembershipsValidity.Add(2, DateTime.Now.AddYears(2));”

Note: I am using Kentico version 9.

Correct Answer

Peter Mogilnitski answered on July 26, 2018 16:36

Take a look at the API, you have to write a script to update membership for users you want.

    // Gets user and membership objects
UserInfo user = UserInfoProvider.GetUserInfo("Username");
MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);

if ((user != null) && (membership != null))
{
    // Grants the membership to the user
    MembershipUserInfoProvider.AddMembershipToUser(membership.MembershipID, user.UserID, DateTime.Now.AddYears(2));
}
1 votesVote for this answer Unmark Correct answer

Recent Answers


anand kamane answered on July 27, 2018 12:30

@Peter Mogilnitski Thanks it works :)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.