Showing posts with label Sharepoint. Show all posts
Showing posts with label Sharepoint. Show all posts

Tuesday, 9 February 2016

SharePoint solution activate button is disabled

Hi,

Today we are working on integrating CRM with SharePoint. As we all know if we need to upload list component in SharePoint and activate it. When I tried upload, observed that the Activate button is disabled even though I logged in as Admin.

The problem is that "SharePoint User Code Host" service was disabled which was of course due to the "Microsoft SharePoint Foundation Sandboxed Code Service" being Stopped (or probably in this case just never started to begin with).

To start the service, navigate to Open Central Administration--> System Settings --> Manage Services on Server and start "Microsoft SharePoint Foundation Sandboxed Code Service".
 
Hope it helps.

--
Happy CRM'ing
Gopinath

Wednesday, 7 October 2015

Create a folder in SharePoint from CRM Plugin

Hi,
 
Today we got requirement to create a folder automatically in Share Point when a record is created in CRM. Normally SharePoint document location is created when we click on Documents on the left navigation pane for the record.
 
It can be done very easily by using the plugin but we should register the plugin None mode. It's not applicable for Online CRM.
 
Here is the code. 

public void Execute(IServiceProvider serviceProvider)
{
      IPluginExecutionContext iContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
      IOrganizationServiceFactory iServFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
      IOrganizationService iService = iServFactory.CreateOrganizationService(iContext.UserId);
      ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
      if (iContext.InputParameters.Contains("Target") && iContext.InputParameters["Target"] is Entity && iContext.MessageName.ToLower() == "create")
      {
           Entity entAccount = (Entity)iContext.InputParameters["Target"];
           string strSharePointURL = CreateFolderInSharePoint(iService, entAccount.Attributes["name"].ToString(), entAccount.Id);
      }
}

public string CreateFolderInSharePoint(IOrganizationService coreCrmService, string strAccountName, Guid guidAccountId)
{
      string strDocLibraryUrl = "http://SharePointURL";
      string strApplicationFolderUrl = strDocLibraryUrl + "/" + "account";
      string strFolderUrl = strApplicationFolderUrl + "/" + strAccountName;
      // Create folder and subfolder inside SharePoint site
      CreateFolderStructure(strApplicationFolderUrl, strFolderUrl);
      // create and associate the url of the folder to SharePointDocumentLocation and the crm record.
      CreateAndAssociateSharePointDocumentLocation(coreCrmService, guidAccountId, "account", strFolderUrl);
      return strFolderUrl;
}

// Check and create folder.
private bool CreateFolderStructure(string strSharePointDocumentUrl, string strFolderUrl)
{
     if (CreateFolder(strSharePointDocumentUrl))
     {
          return CreateFolder(strFolderUrl);
     }
     return false;
}

// Create Folder
private bool CreateFolder(string strFolderUrl)
{
     try
     {
          WebRequest request = WebRequest.Create(strFolderUrl);
          request.UseDefaultCredentials = true;
          request.Method = "MKCOL";
          WebResponse response = request.GetResponse();
          response.Close();
     }
     catch (Exception ex)
     {
           // throw ex;
     }
     return true;
}

// Create a Document Location record in CRM by associating Share Point URL.
public static bool CreateAndAssociateSharePointDocumentLocation(IOrganizationService iService, Guid guidIncidentId, string strAccount, string strSharePointDocumentUrl)
{
      Entity entSharePointDocumentLocation = new Entity("sharepointdocumentlocation");
      entSharePointDocumentLocation.Attributes["name"] = "SharePoint Document Location for " + strAccount;
      entSharePointDocumentLocation.Attributes["description"] = "SharePoint Document Location created for storing documents related to account";
      entSharePointDocumentLocation.Attributes["absoluteurl"] = strSharePointDocumentUrl;
      entSharePointDocumentLocation.Attributes["regardingobjectid"] = new EntityReference("account", guidIncidentId);
      iService.Create(entSharePointDocumentLocation);
      return true;
}
 
Hope this helps.
 
--
Happy CRM'ing
Gopinath

Monday, 9 March 2015

Read Sharepoint file(s) inside a folder

Read SharePoint file(s) in C#

Today I got a requirement to read a document inside a folder. I have integrated CRM with SharePoint and once we upload a document, list component creates a folder with the title appended with GUID of the record and places the files inside.

Here is the code to read the files from that folder.


using (ClientContext clientContext = new ClientContext(SharepointsiteURL))
            {
                clientContext.Credentials = new System.Net.NetworkCredential(SharepointUsername, Sharepointpassword, "<Domain>");
                Web objWeb = clientContext.Web;
                List douLib = objWeb.Lists.GetByTitle("Case");
                clientContext.Load(objWeb);
                CamlQuery caml = new CamlQuery();
                string strQuery = "<View Scope=\"RecursiveAll\"> " +
                    "<Query>" +
                    "<Where>" +
                                "<Eq>" +
                                    "<FieldRef Name=\"FileDirRef\" />" +
                                    "<Value Type=\"Text\"><Share Point Folder Relative URL></Value>" +
                                 "</Eq>" +
                    "</Where>" +
                    "</Query>" +
                    "</View>";
                caml.ViewXml = strQuery;
                ListItemCollection items = douLib.GetItems(caml);
                clientContext.Load(items);
                clientContext.ExecuteQuery();
                WebRequest objWebRequest;
                MemoryStream objMemStream;
                WebResponse objResponse;
                Stream fileStream;
                for (int intCount = 0; intCount < items.Count; intCount++)
                {
                    // Get the path of
                    objWebRequest = WebRequest.Create(new Uri("<FileURL>"));
                    objWebRequest.Credentials = new System.Net.NetworkCredential(SharepointUsername, Sharepointpassword, "<Domain>");
                   objMemStream = new MemoryStream();
                    try
                    {
                        objResponse = objWebRequest.GetResponse();
                        fileStream = objResponse.GetResponseStream() as Stream;
                        fileStream.CopyTo(objMemStream);
                        objMemStream.Position = 0;
                        byte[] objByteArray = objMemStream.ToArray();
                       // System.IO.File.WriteAllBytes(@"\test.xlsx", objByteArray);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

--
Happy CRM'ing
Gopinath.