# PlaylistManagement [C#]

This example shows how to retrive groups playlists and medias from specific manager, and how to add a new media to playlist.

[C#]

       //Initialize the XAML object and retrieve group from server by login
       public PlaylistManagement(string login, string password)
       {
           InitializeComponent();

           //Login and retrive group
           List<View_Group> listResult;
           sample.GetGroup(out listResult, login, password);

           //Bind group to tree
           treeGroup.ItemsSource = TreeNode.GetTreeNode(listResult);
       }

[C#]

       //Select group and retrive coresponding medias and playlist
       private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
       {
           //Get Medias for specific group
           List<View_Media> listMediaResult;
           sample.GetMedia(out listMediaResult, (treeGroup.SelectedItem as TreeNode).Id??0);

           //Update media thumbnail path
           listMediaResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

           //Bind media to List
           gridMedia.ItemsSource = listMediaResult;

           //Get Playlist for specific group
           List<View_Playlist> listPlaylistResult;
           sample.GetPlaylist(out listPlaylistResult, (new long[] {(treeGroup.SelectedItem as TreeNode).Id ?? 0}).ToList());

           //Bind playlist to List
           gridPlaylist.ItemsSource = listPlaylistResult;
       }

[C#]

       //Select playlist and retrive coresponding playlist medias
       private void gridPlaylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           if (gridPlaylist.SelectedItem == null) return;

           //Get Playlist media from selected playlist
           List<View_PlaylistComponent> listResult;
           sample.GetPlaylistMedia(out listResult, (treeGroup.SelectedItem as TreeNode).Id ?? 0, (gridPlaylist.SelectedItem as View_Playlist).Id);

           //Update media thumbnail path
           listResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

           //Bind media to List
           gridPlComponent.ItemsSource = new ObservableCollection<View_PlaylistComponent>(listResult);
       }

[C#]

       //Drop media to playlist
       private void gridPlComponent_Drop(object sender, DragEventArgs e)
       {
           if (gridPlaylist.SelectedItem == null) return;

           //Retrive list of playlist media
           ListBox box = (ListBox)sender;
           ObservableCollection<View_PlaylistComponent> list = (box.ItemsSource as ObservableCollection<View_PlaylistComponent>);

           //Retrive media to drop
           View_Media media = e.Data.GetData(typeof(View_Media)) as View_Media;

           //Get media position
           Point pnt = e.GetPosition(box);
           ScrollViewer scroll = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(box, 0), 0) as ScrollViewer;
           int position = Math.Min(list.Count, (int)(scroll.VerticalOffset + (pnt.Y / 48)));

           //Create playlist media data
           View_PlaylistComponent component = new View_PlaylistComponent()
               {
                   //Active = media.Active,
                   //Duration = media.Duration,
                   //HideBanner = media.HideBanner,
                   ManagerIdAddedBy = media.ManagerIdAddedBy,
                   MediaId = media.Id,
                   MediaName = media.Name,
                   //MediaPath = media.MediaPath,
                   NoBanner = true,
                   Order = position,
                   //PeriodCount = media.periodCount ?? 0,
                   PlaylistId = (gridPlaylist.SelectedItem as View_Playlist).Id,
                   ThumbnailPath = media.ThumbnailPath,
                   TypeId = 1
               };

           //Add playlist media
           sample.AddPlaylistMedia(ref component, (treeGroup.SelectedItem as TreeNode).Id ?? 0);

           //Insert result in the collection
           list.Insert(position, component);
       }