Sunday, July 10, 2016

SharePoint Hybrid Posts

Hi, Currently working on implementation of Hybrid Search with current environment of SP 2013 and SharePoint Online Tenant. Will share my experience soon here.

Thursday, October 1, 2015

Connections to the SharePoint server are currently disabled because the project is in Offline mode


Developing SharePoint hosted app, if somehow you are not able to connect with your tenant site and provide to work in offline mode and your project go to offline mode in Visual Studio. Now to come back online to  deploy your app:

Open SharePoint project
Select solution
Click F4
You will see: Server Connection
Select : Online

Cheerssssssssss :)

Monday, July 20, 2015

SharePoint Online - Create a SharePoint Group dynamically based on the permission type to all SharePoint Sites

Scenario - Suppose you have a SharePoint site collection and you would like to add a new group to all sites and sub-sites (Unique Permissions). 

The following code creates a group to all sites based on the permission type and name it with "SITENAME_PERMISSION". You can change it to any parameter.


using (ClientContext clientContextSub = new ClientContext(strSiteUrl))
            {
                SecureString passWord = new SecureString();
                foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
     clientContextSub.Credentials = new SharePointOnlineCredentials(userName, passWord);
                clientContextSub.ExecuteQuery();
                try
                {

                 Web webSubWeb = clientContextSub.Web;
                 GroupCollection groupCollection = webSubWeb.SiteGroups;
                 clientContextSub.Load(webSubWeb.RoleAssignments);
                 clientContextSub.Load(
                 webSubWeb,
                   web2SubWebSubNode => web2SubWebSubNode.Title,
                   web2SubWebSubNode => web2SubWebSubNode.Url,
                   web2SubWebSubNode => web2SubWebSubNode.LastItemModifiedDate,
                   web2SubWebSubNode => web2SubWebSubNode.HasUniqueRoleAssignments);
                   clientContextSub.ExecuteQuery();
                  
                    if (webSubWeb.HasUniqueRoleAssignments)
                    {
                        bool _permissionGroupAvailable = false;
                        string _groupNameToAssign = string.Empty;
                        string groupOwener = string.Empty;
                        foreach (RoleAssignment assignment in webSubWeb.RoleAssignments)
                        {
                            try
                            {
                                string Role = string.Empty;
                                clientContextSub.Load(assignment.Member);
                                clientContextSub.Load(assignment.RoleDefinitionBindings);
                                clientContextSub.ExecuteQuery();

                    foreach (RoleDefinition roleDef in assignment.RoleDefinitionBindings)
                                {
//CMBAddGroup is selected permission type from a dropdown (Ex- Full Control, Read, Contribute)
       if (roleDef.Name.ToString().Trim() == cmbAddGroup.SelectedItem.ToString().Trim())
                                    {
                                        _permissionGroupAvailable = true;
                                        break;
                                    }

                                }
                                if (_permissionGroupAvailable)
                                { break; }

                            }

                            catch (Exception ex)
                            {
                               continue;
                            }
                        }
                        if (!(_permissionGroupAvailable))
                        {
                           
                           {
_groupNameToAssign = webSubWeb.Title + "_" + cmbAddGroup.SelectedItem.ToString();
GroupCreationInformation newGroupInfo = new GroupCreationInformation();
newGroupInfo.Title=_groupNameToAssign.ToString();
Group groupExist = webSubWeb.SiteGroups.GetByName(_groupNameToAssign);
                            if (groupExist == null)
                            {
Group oGroup = webSubWeb.SiteGroups.Add(newGroupInfo);
RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContextSub);
RoleDefinition oRoleDefinition = webSubWeb.RoleDefinitions.GetByName(cmbAddGroup.SelectedItem.ToString());
collRoleDefinitionBinding.Add(oRoleDefinition);
webSubWeb.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
clientContextSub.Load(oGroup, group => group.Title);
clientContextSub.Load(oRoleDefinition,role => role.Name);
clientContextSub.ExecuteQuery();
                            }
                            else
                            {
RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContextSub);
RoleDefinition oRoleDefinition = webSubWeb.RoleDefinitions.GetByName(cmbAddGroup.SelectedItem.ToString());
collRoleDefinitionBinding.Add(oRoleDefinition);
webSubWeb.RoleAssignments.Add(groupExist, collRoleDefinitionBinding);
clientContextSub.Load(groupExist, group => group.Title);
clientContextSub.Load(oRoleDefinition,role => role.Name);
clientContextSub.ExecuteQuery();

                            }
                            }
                            }

                       
                        }
                    }

            

                catch (Exception ex)
                {
                    helpClass.ErrorLog("", "Error MESSAGE: " + ex.Message);

                }
            }

        }


Wednesday, June 3, 2015

SharePoint Online - How to get size of a site collection Recycle Bin - First Stage Recycle Bin

Requirement - In SharePoint Online, First Stage Recycle Bin consumes your site quota space. The following code gets how much data is there under the First Stage Recycle Bin.

Note - Always pass Site URL. Web URL will not work and always return 0.

Function getRecycleBinSize takes URL is parameter and stores all results in a data table.

public void getRecycleBinSize(string URL)
        {
            DataTable dtRecycleBin = new DataTable();
            using (ClientContext clientContext = new ClientContext(URL))
            {
                SecureString passWord = new SecureString();
                foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
                clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);
                Site s = clientContext.Site;
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.Load(s);
                clientContext.Load(s.RecycleBin);
                clientContext.ExecuteQuery();
                if(dtRecycleBin.Columns.Count==0)
                {
                dtRecycleBin.Columns.Add(constantVariables.Title);
                dtRecycleBin.Columns.Add(constantVariables.deletedBy);
                dtRecycleBin.Columns.Add(constantVariables.deletionDate);
                dtRecycleBin.Columns.Add(constantVariables.location);
                dtRecycleBin.Columns.Add(constantVariables.itemState);
                dtRecycleBin.Columns.Add(constantVariables.itemType);
                dtRecycleBin.Columns.Add(constantVariables.leafName);
                dtRecycleBin.Columns.Add(constantVariables.size);
                }
             
                RecycleBinItemCollection recyclebinItemCollection = s.RecycleBin;
                clientContext.Load(recyclebinItemCollection);
                clientContext.ExecuteQuery();
                int count = recyclebinItemCollection.Count;
                int i = 0;
                foreach (RecycleBinItem item in recyclebinItemCollection)
                {                  
                   
                    if (item.ItemState.ToString().ToLower() == "FirstStageRecycleBin".ToLower())
                    {
                        DataRow newRow = dtRecycleBin.NewRow();
                        dtRecycleBin.Rows.Add(newRow);
                        newRow[0] =  item.Title.ToString() ;
                        newRow[1] =  item.DeletedBy.ToString();
                        newRow[2] =  item.DeletedDate ;
                        newRow[3] =  item.DirName ;
                        newRow[4] =  item.ItemState ;
                        newRow[5] =  item.ItemType ;
                        newRow[6] =  item.LeafName;
                        newRow[7] =  item.Size ;
                    }
                    i++;
                                   
                }
            }
            dataGridView2.DataSource = null;
            dataGridView2.DataSource = dtRecycleBin;
        }

Thursday, May 28, 2015

SharePoint Online - Add User to SharePoint Security Group with a specific permission

The following code can be used to add user to a specific group.

Requirement - Business want to add a specific user to a site collection and all it's sub-site in a specific group. Ex: Full Control or Read or Edit.


using (ClientContext clientContextSub = new ClientContext(siteUrl))
                        {
                            SecureString passWord = new SecureString();
                            foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
                clientContextSub.Credentials = new SharePointOnlineCredentials(userName, passWord);
                            Web webSubWeb = clientContextSub.Web;
                            GroupCollection groupCollection = clientContextSub.Web.SiteGroups;
                            UserCollection ucol = clientContextSub.Web.SiteUsers;
                            clientContextSub.Load(webSubWeb);
                            clientContextSub.ExecuteQuery();

                           //txtAddUserToGroup is a text file to add to groups
                            Principal user = ucol.GetByEmail(txtAddUserToGroup.Text.ToString().Trim());
                            clientContextSub.Load(user);
                            clientContextSub.ExecuteQuery();


// dtGroupUsers is a datatable logging who's being added to which group with other information
                            if (dtGroupandUsers.Rows.Count == 0)
                            {
                                if(dtGroupandUsers.Columns.Count==0)
                                {
                                dtGroupandUsers.Columns.Add(constantVariables.SiteURL);
                                dtGroupandUsers.Columns.Add(constantVariables.Title);
                                dtGroupandUsers.Columns.Add(constantVariables.userGroups);
                                dtGroupandUsers.Columns.Add(constantVariables.userLogin);
                                dtGroupandUsers.Columns.Add(constantVariables.emailAddress);
                                dtGroupandUsers.Columns.Add(constantVariables.name);
                                dtGroupandUsers.Columns.Add(constantVariables.LastModified);
                                dtGroupandUsers.Columns.Add(constantVariables.siteInheritance);
                                dtGroupandUsers.Columns.Add(constantVariables.groupOwner);
                                dtGroupandUsers.Columns.Add(constantVariables.permission);
                                }
                            }
                            else
                            { dtGroupandUsers.Rows.Clear(); }          
                            if (user != null)
                            {
                                addUsertoGroup(webSubWeb, clientContextSub,user);
                             
                            }
                            }
                    }

///***Other Function ***///
public void addUsertoGroup(Web webSubWeb, ClientContext clientContextSub, Principal user)
        {
string groupOwener = string.Empty;
            clientContextSub.Load(webSubWeb);
            clientContextSub.Load(webSubWeb.Webs);
            clientContextSub.Load(webSubWeb.RoleAssignments);
            clientContextSub.ExecuteQuery();
            foreach (RoleAssignment assignment in webSubWeb.RoleAssignments)
            {
                string Role = string.Empty;
                clientContextSub.Load(assignment.Member);
                clientContextSub.Load(assignment.RoleDefinitionBindings);
                GroupCollection groupCollection = webSubWeb.SiteGroups;
                clientContextSub.ExecuteQuery();
                foreach (RoleDefinition roleDef in assignment.RoleDefinitionBindings)
                {
                    if (roleDef.Name != string.Empty)
                    {
                       //cmbUserCollection.Text  is a dropdown control and user has selected "Full Control" and any group having permission "Full Control" with add this user.

      if (roleDef.Name.ToString().ToLower() == cmbUserCollection.Text.ToString().ToLower())
                        {
                            int Cnt = assignment.RoleDefinitionBindings.Count;
                            Principal Pri = assignment.Member;
                            if (Pri.PrincipalType == PrincipalType.SharePointGroup)
                            {                              
                                UserCreationInformation userCreationInfo = new UserCreationInformation();
                                userCreationInfo.Email = user.LoginName;
                                userCreationInfo.LoginName = user.LoginName;
                                userCreationInfo.Title = user.Title;
                                Group grp = groupCollection.GetByName(Pri.LoginName);
                                clientContextSub.Load(grp);
                                clientContextSub.ExecuteQuery();
                               
                                grp.Users.Add(userCreationInfo);
                             
                                    DataRow dtGU = dtGroupandUsers.NewRow();
                                    dtGroupandUsers.Rows.Add(dtGU);
                                    dtGU[0] = "\"" + webSubWeb.Url.ToString() + "\"";
                                    dtGU[1] = "\"" + webSubWeb.Title.ToString() + "\"";
                                    dtGU[2] = "\"" + grp.Title.ToString() + "\"";
                                    dtGU[3] = "\"" + user.LoginName.ToString() + "\"";
                                    if (user.LoginName.Contains('#'))
                                    {
dtGU[4] = "\"" + user.LoginName.Substring(18, (user.LoginName.Length - 18)).Trim() + "\""; }
                                    dtGU[5] = "\"" + user.Title.ToString() + "\"";
                                    dtGU[6] = "\"" + webSubWeb.LastItemModifiedDate;
                                    dtGU[7] = "";
                                    dtGU[8] = "\"" + grp.OwnerTitle.ToString() + "\"";
                                    dtGU[9] = roleDef.Name.ToString();
                            }              
                        }            
                    }
                }
               
            }
           
            if (webSubWeb.Webs.Count > 0)
            {
                foreach (Web web in webSubWeb.Webs)
                {
                    addUsertoGroup(web, clientContextSub, user);

                }
           
            }}

Monday, February 23, 2015

Adding Content Menu and Custom Menu items on a tree node - Windows Application using C#

This code shows how you can add a context menu with the context menu items to your window application on a tree node.

ToolStripMenuItem siteSettings = new ToolStripMenuItem();
            siteSettings.Text = "Site Settings";
            siteSettings.BackColor = Color.AliceBlue;
            siteSettings.Image = imageList1.Images[5];

            ToolStripMenuItem lstContentTypeReport = new ToolStripMenuItem();
            lstContentTypeReport.Text = "Lists Content Types";
            lstContentTypeReport.BackColor = Color.AliceBlue;
            lstContentTypeReport.Image = imageList1.Images[5];

            ToolStripMenuItem treeStructure = new ToolStripMenuItem();
            treeStructure.Text = "Site Structure";
            treeStructure.BackColor = Color.AliceBlue;
            treeStructure.Image = imageList1.Images[15];


            ToolStripMenuItem manageAuditSettings = new ToolStripMenuItem();
            manageAuditSettings.Text = "Manage Audit Settings";
            manageAuditSettings.BackColor = Color.AliceBlue;
            manageAuditSettings.Image = imageList1.Images[15];

            ToolStripMenuItem manageSPAlerts = new ToolStripMenuItem();
            manageSPAlerts.BackColor = Color.AliceBlue;
            manageSPAlerts.Text = "Manage ShaerPoint Alerts";
            manageSPAlerts.Image = imageList1.Images[14];

            ToolStripMenuItem addUsers = new ToolStripMenuItem();
            addUsers.Text = "Add Users";
            addUsers.BackColor = Color.AliceBlue;
            addUsers.Image = imageList1.Images[13];

            ToolStripMenuItem siteStorageAnalysis = new ToolStripMenuItem();
            siteStorageAnalysis.Text = "Site Storage Analysis";
            siteStorageAnalysis.BackColor = Color.AliceBlue;
            siteStorageAnalysis.Image = imageList1.Images[12];

            ToolStripMenuItem sitePemissions = new ToolStripMenuItem();
            sitePemissions.Text = "Site Permissions";
            sitePemissions.BackColor = Color.AliceBlue;
            sitePemissions.Image = imageList1.Images[11];

            ToolStripMenuItem findUserSecurity = new ToolStripMenuItem();
            findUserSecurity.Text = "Find User Security";
            findUserSecurity.BackColor = Color.AliceBlue;
            findUserSecurity.Image = imageList1.Images[10];
            contextMenuStrip2.Items.AddRange(new ToolStripMenuItem[] { siteSettings, lstContentTypeReport, manageAuditSettings, treeStructure, manageSPAlerts, addUsers });
            contextMenuStrip2.Items.Add(new ToolStripSeparator());
            contextMenuStrip2.Items.AddRange(new ToolStripMenuItem[] { siteStorageAnalysis, sitePemissions, findUserSecurity });



                Treenode Control:


                         TreeNode Groups = new TreeNode("Sites");                     
                  

                        Groups.ContextMenuStrip = groupContextMenu;

SharePoint Online - Connect and fetch information usign client side object model (C#)

Below code example shows how to connect to a tenant application and iterate through with all the site collections.

Tip: Assuming you have permission to read content to all site collection under this tenant.
 If not, use proper try catch with continue option. J

Step 1 - Add reference for the following assemblies. 

using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

Step 2 -
string userName = "";
string passWord = "";

using (ClientContext clientContext = new ClientContext(txtTenantUrl.Text))
            {

                SecureString passWord = new SecureString();
           foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
           clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);

                Tenant tenant = new Tenant(clientContext);
                SPOSitePropertiesEnumerable spp = tenant.GetSiteProperties(0, true);
                clientContext.Load(spp);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.Load(web.Webs);
                clientContext.Load(clientContext.Web.SiteGroups);
                clientContext.Load(clientContext.Web.SiteUsers);
                clientContext.ExecuteQuery();

                foreach (SiteProperties sp in spp)
                {
                   
                              string owner = sp.Owner.ToString();
                    string URL = sp.Url.ToString();
                    string webCount = sp.WebsCount.ToString();
                    string webLastModified = sp.LastContentModifiedDate.ToString();
                    string webStatus = sp.Status.ToString();
                    string webStorage = sp.StorageUsage.ToString();
                    string level = sp.StorageMaximumLevel.ToString();
string contentLastModified = sp.LastContentModifiedDate.ToString();
                }
               
            }

Tuesday, March 5, 2013

Activating Napa App for Office Online SharePoint 2013 Development



So you are all set and got the free app called NAPA but it's keep on showing you with the message that you can not add this app here.So what's going wroing????

So here is what you should do.

Just add the “Napa” Office 365 Development Tools app to your SharePoint Online Developer Site, launch it, and you will be ready to create your first app for Office or SharePoint.

To enable Napa for App Development, please follow below steps

• In your Office 365 Admin Panel, choose SharePoint

• Create a Private Site Collection with Developer Site template.

• Browse to the newly create Site Collection and then select on first tile “Build an App”

• Now Click on ADD IT and Trust it and you are good to go.

Cheers!!! Suneet

Friday, February 22, 2013

Merge multiple documents in SharePoint 2010 Programmatically

So here is the scenario:

 
1.  SharePoint document library has multiple documents with the same context let’s say month’s announcements.

2.  Now a single newsletter should go to all users with all announcements in a single announcement monthly letter.

3.  So it’s nothing but merging multiple documents into a single document.

 

Note:
Here “filepath1” is the location of the first document and “filepath2” is for second document. So it could be like http:/// Lib>/documentname.docx

Hope you are clear here as this code only merge 2 documents…but with a loop you can have multiple documents.


________________________________________________________________________________
private void readDocument(string filePath1, string filePath2)

        {
            string targetfile = string.Empty;
            object file1 = filePath1;
            object file2 = filePath2;
            object file3 = targetfile;
            object objMissing = System.Reflection.Missing.Value;
            object objTrue = System.Reflection.Missing.Value;
            object start = 0;
            object end = 0;

          

ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();


Document finalDocument = WordApp.Documents.Add(ref objMissing, ref objMissing, ref objMissing, ref objMissing);
 

//Range field will ensure they won’t overwrite and second document start where first document ends.


Range rng = finalDocument.Range(ref start, ref objMissing);

rng.InsertFile(filePath1, ref objMissing, ref objMissing, ref objMissing, ref objMissing);

start = (WordApp.ActiveDocument.Content.End)-1; 

Range rng1 = finalDocument.Range(ref start, ref objMissing); 

rng1.InsertFile(filePath2, ref objMissing, ref objMissing, ref objMissing, ref objMissing); 

start = (WordApp.ActiveDocument.Content.End) - 1; 

finalDocument.Save();         

}

 

Cheers!!!

Suneet

Monday, August 27, 2012

Clear your recycle-bin with PowerShell.

My team recently developed this script....it'll clear your recycle-bin with PowerShell & no need to do it 200 at a time.

$url = "http://your site name/sites/site";
$siteCollection = New-Object Microsoft.SharePoint.SPSite($url);
$siteCollection.RecycleBin.Count;
$siteCollection.RecycleBin.DeleteAll();
$siteCollection.Dispose();

Cheers!!!