London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

removing link item from link item collection programmatically

Vote:
 

Hi, 

I am using episever cms 6 r2 version. 

I am trying to remove a link item from link item collection property and save the page. But that link is not removed and still there. 

Any help is appreciated. 

 

[WebMethod]
public void RemovePrivateLink(string privatelink)
{
UserService userservice = new UserService();
CurrentUserExtensions currentLoginUser = new CurrentUserExtensions();
var user = userservice.GetUserProfile(currentLoginUser.Signum);
if (user != null)
{
var userPage = (EmployeePage)user.Page.CreateWritableClone();
if (userPage != null)
{

LinkItem objLinkItem = new LinkItem();
objLinkItem.Href = privatelink;

//removing private link from the collection
userPage.PrivateLinks.Remove(objLinkItem);

var currentUser = HttpContext.Current.User;
//read value from web.config
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["impersonateuser"];
var adminaccount = setting.Value;

//run as another user
System.Web.Security.MembershipUser muuser = System.Web.Security.Membership.Provider.GetUser(adminaccount, false);
System.Security.Principal.GenericIdentity identity = new System.Security.Principal.GenericIdentity(muuser.UserName, muuser.ProviderName);
GenericPrincipal principal = new GenericPrincipal(identity, System.Web.Security.Roles.GetRolesForUser(muuser.UserName));
HttpContext.Current.User = principal;
System.Threading.Thread.CurrentPrincipal = principal;

//save the page
EPiServer.DataFactory.Instance.Save(userPage, EPiServer.DataAccess.SaveAction.Publish);
}
}

}

#75740
Oct 04, 2013 13:12
Vote:
 

Fixed this on my own !!

 

//Remove trailing '/' character in the end
privatelink= privatelink.Remove(privatelink.LastIndexOf("/"));

var matchingPrivateLinkCounter=userPage.PrivateLinks.Where(x => x.Href == privatelink).Count();

if ( matchingPrivateLinkCounter> 0)
{

var objLinkItem = userPage.PrivateLinks.Where(x => x.Href == privatelink).Single();

//removing private link from the collection
userPage.PrivateLinks.Remove(objLinkItem);

}

#75832
Oct 08, 2013 13:46
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.