Thursday, August 25, 2011

Client Object Model – Part 11

We are going to continue exploring more Silverlight Client Object model examples. Again, same examples can be used for managed client object model as well. It’s just that Silverlight works on delegate methods for asynchronous methods. While working with managed object model is quite simpler.

If you have not gone through Part 1 to Part 10, I would recommend you reading them first and then continue exploring from here. There is a lot of stuff to learn in those parts.

1) Break Role Inheritance

If you want to break role inheritance for a list or any list item or library or document or a sub site, we can use BreakRoleInheritance(Boolean, Boolean) method to break it. Breaking inheritance means stopping permission inheriting from its parent and then assigning unique permissions for specific object.



If first Boolean parameter which is CopyRoleAssignments is false then it even does not copy any permission from parent and user becomes an owner of that object under whom the code is running. Second parameter which is clearSubScopes means that do we need to clear the unique permission on child object and then subsequently inherit permission from its parent or not.




namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();


public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
BreakInheritance();
}

private void BreakInheritance()
{
context = ClientContext.Current;

web = context.Web;

List lstEmployee = web.Lists.GetByTitle("Employees");

lstEmployee.BreakRoleInheritance(true, true);

context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{
lblOutputLabel.Text = "Inheritance has been broken successfully from its parent site";
}


private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}

private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}


}
}



2) Custom User Actions



Custom user actions mean any action that is added to the default options given in the SharePoint. We can add edit control block as custom action which reflects an option while we select the list item or document item in list or library. We can define custom action to be in the menu options of site actions menu. We can also define custom actions options for modify all site settings sub menu options.


UserCustomActions returns the collection of all custom actions defined in an object. If your object is list, then it returns all custom actions defined in that list. If your object is site, then it returns all custom actions defined at site level.

We can add, remove and update custom actions through client object model. Whatever we can do with one client object model method can be achieved with other client object model methods.

Adding custom user actions to a list edit control block


context = ClientContext.Current;

web = context.Web;

List lstEmployee = web.Lists.GetByTitle("Employees");

UserCustomActionCollection collCustomAction = lstEmployee.UserCustomActions;

UserCustomAction UserCustomAction = collCustomAction.Add();
UserCustomAction.Location = "EditControlBlock";
UserCustomAction.Sequence = 100;
UserCustomAction.Title = "SPKings User Custom Action";
UserCustomAction.Url = context.Url + @"/_layouts/SPKingsPage.aspx";
UserCustomAction.Update();


context.ExecuteQueryAsync(LoadSucces, LoadFailed);



Update custom user actions to a list edit control block


namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();

UserCustomActionCollection collCustomAction;

public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
AddCustomAction();
}


private void AddCustomAction()
{
context = ClientContext.Current;

web = context.Web;

List lstEmployee = web.Lists.GetByTitle("Employees");

collCustomAction = lstEmployee.UserCustomActions;

context.Load(collCustomAction,
userCustomActions => userCustomActions.Include(
userCustomAction => userCustomAction.Title));

context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{

foreach (UserCustomAction oUserCustomAction in collCustomAction)
{
if (oUserCustomAction.Title == "SPKings User Custom Action")
{
oUserCustomAction.ImageUrl = context.Url + "/_layouts/images/SomeImage.jpeg";
oUserCustomAction.Update();

context.ExecuteQueryAsync(LoadSuccesAfterUpdate, LoadFailed);
}
}

}

private void DisplayMessage()
{
lblOutputLabel.Text = "Custom Action - ECB has been update successfully";
}

private void LoadSuccesAfterUpdate(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUIAfterUpdate = DisplayMessage;
this.Dispatcher.BeginInvoke(updateUIAfterUpdate);
}

private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}

private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
}


}
}




3) Working with Web Parts on a page

We can get all the web parts residing on a page with the help of client object model and re arrange them as well as change their properties like title. We can also remove them or add new one as well.

Change title of the web part


namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
LimitedWebPartManager limitedWebPartManager = null;

public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
AddCustomAction();
}


private void AddCustomAction()
{
context = ClientContext.Current;

string URL = ClientContext.Current.Url;

web = context.Web;

File HomePage = web.GetFileByServerRelativeUrl("/sites/SP2010/Shared%20Documents/Default.aspx");


limitedWebPartManager = HomePage.GetLimitedWebPartManager(PersonalizationScope.Shared);

context.Load(limitedWebPartManager.WebParts,
webparts => webparts.Include(
webpart => webpart.WebPart.Title));


context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{

WebPartDefinition oWebPartDefinition = limitedWebPartManager.WebParts[1];
WebPart oWebPart = oWebPartDefinition.WebPart;
oWebPart.Title = "SPKings Title Changed again";

oWebPartDefinition.SaveWebPartChanges();

context.ExecuteQueryAsync(LoadSuccesAfterUpdate, LoadFailed);


}

private void DisplayMessage()
{
lblOutputLabel.Text = "WebPart title has been changed successfully";
}

private void LoadSuccesAfterUpdate(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUIAfterUpdate = DisplayMessage;
this.Dispatcher.BeginInvoke(updateUIAfterUpdate);
}

private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}

private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);

}


}
}




Deleting a web part


namespace SilverlightSPCOM
{
public partial class MainPage : UserControl
{
private ClientContext context = null;
private Web web = null;
private delegate void UpdateUIMethod();
LimitedWebPartManager limitedWebPartManager = null;

public MainPage()
{
InitializeComponent();
}

private void btnRetrive_Click(object sender, RoutedEventArgs e)
{
AddCustomAction();
}


private void AddCustomAction()
{
context = ClientContext.Current;

string URL = ClientContext.Current.Url;

web = context.Web;

File HomePage = web.GetFileByServerRelativeUrl("/sites/SP2010/Shared%20Documents/Default.aspx");

limitedWebPartManager = HomePage.GetLimitedWebPartManager(PersonalizationScope.Shared);

context.Load(limitedWebPartManager.WebParts);

context.ExecuteQueryAsync(LoadSucces, LoadFailed);

}

private void DisplayData()
{

WebPartDefinition webPartDefinition = limitedWebPartManager.WebParts[1];
webPartDefinition.DeleteWebPart();
context.ExecuteQueryAsync(LoadSuccesAfterUpdate, LoadFailed);

}

private void DisplayMessage()
{
lblOutputLabel.Text = "WebPart has been deleted successfully";
}

private void LoadSuccesAfterUpdate(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUIAfterUpdate = DisplayMessage;
this.Dispatcher.BeginInvoke(updateUIAfterUpdate);
}

private void LoadSucces(object sender, ClientRequestSucceededEventArgs e)
{
UpdateUIMethod updateUI = DisplayData;
this.Dispatcher.BeginInvoke(updateUI);
}

private void LoadFailed(object sender, ClientRequestFailedEventArgs e)
{
//MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);

lblOutputLabel.Text = "Request Failed: " + e.Message; // +"Stack Trace:" + e.StackTrace;
}


}
}





Read more in Part-12

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com