Saturday, October 12, 2013

List Item Attachments in Gridview

This code snippet for showing the SharePoint List Item's attachment collection in a ASP.NET. Build your ASP.NET grid view with columns and a Link to delete the selected rows value. Here I am listing out the few files and wanted to allow the user to delete the file by clicking the link. Here is code to retrieve the selected row index and its cell value.
 public void LinkAction(object sender, EventArgs e)
        {


            LinkButton lnkbtn = sender as LinkButton;
            GridViewRow selectedRow = lnkbtn.NamingContainer as GridViewRow;

            int a = selectedRow.RowIndex;
            string txt = selectedRow.Cells[2].Text.ToString();
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    var list = web.Lists["Region"];
                    web.AllowUnsafeUpdates = true;
                    SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders["5"];
                    SPFile file = web.GetFile(web.Url +"/"+folder+"/" + txt);
                    file.Delete();
                    web.Update();
                    web.AllowUnsafeUpdates = false;
                }

            }
  
Grid View Markup










Binding the Grid view with List Item's attachments.
    public List GetAttachments()
        {
            List fNAme = new List();
            
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string sp = string.Empty;
                        var list = web.Lists["Region"];
                        SPListItem item = list.GetItemById(5);
                        SPAttachmentCollection cols = item.Attachments;
                        SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[item.ID.ToString()];
                        FileDetails f = null;
                        foreach (SPFile file in folder.Files)
                        {
                            f = new FileDetails();
                            f.fName = file.Name;
                            f.fUrl = web.Url + "/" + folder.ToString() + "/" + file.Name;
                            fNAme.Add(f);

                        }

                    }

                }
            
            return fNAme;
        }
For Getter and Setter and FileDetails Class code available on my previous post.

Tuesday, October 08, 2013

How to differentiate User and Group in People Editor Control

Here is an simple workaround on " How to differentiate the SPUser and SPGroup from the People Editor Control selection " in SharePoint.


public void ParseUserAndGroup(object sender, EventArgs e)
        {
            List userList = new List();
            List groupList = new List();

            string value = myppl.CommaSeparatedAccounts;
            string[] values = value.Split(',');

            foreach (string s in values)
            {
               if(SPUtility.IsLoginValid(SPContext.Current.Site,s))
               {
                    SPUser user = SPContext.Current.Web.EnsureUser(s);
                    userList.Add(user);
               }
               else
               {
                    SPGroup oGroup = SPContext.Current.Web.SiteGroups[s];
                   groupList.Add(oGroup);
               }

              }

                grdGroup.DataSource = groupList;
                grdGroup.DataBind();
                grdUser.DataSource = userList;
                grdUser.DataBind();
            }

        }

Monday, October 07, 2013

Item Level Permission in SharePoint

Item level permission will be set after that item has been created or updated. When you set item level permission on updating the item you need to remove all the permission and then update the custom permission on the clean slate. Its always best to wire up the permission on the item on "List Item Event Receiver". Code snippet for adding new item level permission
                    var list = webSite.Lists["Region"];
                    SPListItem item = list.Items.GetItemById(4);
                    webSite.AllowUnsafeUpdates = true;
                    SPUser user = webSite.EnsureUser(@"Global\dhanyata");
                    item.BreakRoleInheritance(false);
                    SPRoleDefinitionCollection roleDef = webSite.RoleDefinitions;
                    SPRoleAssignment roleAssignment= new SPRoleAssignment(user);
                    roleAssignment.RoleDefinitionBindings.Add(roleDef["MyCustomPermission"]);
                    item.RoleAssignments.Add(roleAssignment);
                    item.Update();   
            
I have created the custom permission level to be set on item. IF you want to create this programmatically,you can choose to work SPBasePermission class and then assigning to your user or group.