Hi,
Here is how you can set edit control block on specific list and extend site actions menu by including a custom option in that.
This is equivalent to what we do with customactions tag via feature deployment.
This is simple button click example to achieve these two functionality
This is only for demonstration purpose. Write a logic in your own way.
protected void btnAddCustomAction_Click(Object sender, EventArgs e)
{
SPWeb currentsite = SPContext.Current.Web;
#region "Add custom action to a specific list"
SPList lstOrders = currentsite.Lists.TryGetList("Orders");
if (lstOrders != null)
{
currentsite.AllowUnsafeUpdates = true;
if (lstOrders.UserCustomActions.Count > 0)
{
foreach (SPUserCustomAction action in lstOrders.UserCustomActions)
{
if (action.Name == "Google")
{
action.Delete();
lstOrders.Update();
break;
}
}
}
SPUserCustomAction customaction = lstOrders.UserCustomActions.Add();
customaction.Name = "Google";
customaction.Location = "EditControlBlock";
customaction.ImageUrl = @"\_layouts\IMAGES\CustomImages\google_logo.jpg";
customaction.Url = "https://www.google.com";
customaction.Sequence = 1000;
customaction.Title = "Google";
customaction.Update();
lstOrders.Update();
currentsite.AllowUnsafeUpdates = false;
}
#endregion
#region "Add custom action to a web"
if (currentsite.UserCustomActions.Count > 0)
{
foreach (SPUserCustomAction action in currentsite.UserCustomActions)
{
if (action.Name == "GoogleOnSiteAction")
{
currentsite.AllowUnsafeUpdates = true;
action.Delete();
currentsite.Update();
currentsite.AllowUnsafeUpdates = false;
break;
}
}
}
currentsite.AllowUnsafeUpdates = true;
SPUserCustomAction customactiononsiteactions = currentsite.UserCustomActions.Add();
customactiononsiteactions.Name = "GoogleOnSiteAction";
customactiononsiteactions.Location = "Microsoft.SharePoint.StandardMenu";
customactiononsiteactions.Description = "Takes you to a google site";
customactiononsiteactions.Group = "SiteActions";
customactiononsiteactions.ImageUrl = @"\_layouts\IMAGES\CustomImages\google_logo.jpg";
customactiononsiteactions.Url = "https://www.google.com";
customactiononsiteactions.Sequence = 2000;
customactiononsiteactions.Title = "Google";
customactiononsiteactions.Update();
currentsite.Update();
currentsite.AllowUnsafeUpdates = false;
#endregion
}
Here is how you can set edit control block on specific list and extend site actions menu by including a custom option in that.
This is equivalent to what we do with customactions tag via feature deployment.
This is simple button click example to achieve these two functionality
This is only for demonstration purpose. Write a logic in your own way.
protected void btnAddCustomAction_Click(Object sender, EventArgs e)
{
SPWeb currentsite = SPContext.Current.Web;
#region "Add custom action to a specific list"
SPList lstOrders = currentsite.Lists.TryGetList("Orders");
if (lstOrders != null)
{
currentsite.AllowUnsafeUpdates = true;
if (lstOrders.UserCustomActions.Count > 0)
{
foreach (SPUserCustomAction action in lstOrders.UserCustomActions)
{
if (action.Name == "Google")
{
action.Delete();
lstOrders.Update();
break;
}
}
}
SPUserCustomAction customaction = lstOrders.UserCustomActions.Add();
customaction.Name = "Google";
customaction.Location = "EditControlBlock";
customaction.ImageUrl = @"\_layouts\IMAGES\CustomImages\google_logo.jpg";
customaction.Url = "https://www.google.com";
customaction.Sequence = 1000;
customaction.Title = "Google";
customaction.Update();
lstOrders.Update();
currentsite.AllowUnsafeUpdates = false;
}
#endregion
#region "Add custom action to a web"
if (currentsite.UserCustomActions.Count > 0)
{
foreach (SPUserCustomAction action in currentsite.UserCustomActions)
{
if (action.Name == "GoogleOnSiteAction")
{
currentsite.AllowUnsafeUpdates = true;
action.Delete();
currentsite.Update();
currentsite.AllowUnsafeUpdates = false;
break;
}
}
}
currentsite.AllowUnsafeUpdates = true;
SPUserCustomAction customactiononsiteactions = currentsite.UserCustomActions.Add();
customactiononsiteactions.Name = "GoogleOnSiteAction";
customactiononsiteactions.Location = "Microsoft.SharePoint.StandardMenu";
customactiononsiteactions.Description = "Takes you to a google site";
customactiononsiteactions.Group = "SiteActions";
customactiononsiteactions.ImageUrl = @"\_layouts\IMAGES\CustomImages\google_logo.jpg";
customactiononsiteactions.Url = "https://www.google.com";
customactiononsiteactions.Sequence = 2000;
customactiononsiteactions.Title = "Google";
customactiononsiteactions.Update();
currentsite.Update();
currentsite.AllowUnsafeUpdates = false;
#endregion
}
No comments:
Post a Comment