Tuesday, 29 May 2018

How to adjust / resize SharePoint 2013 List View columns?

You can change the width (or anything) using CSS.
<style type='text/css'>
.ms-vh-div[DisplayName='ColumnName']
{
  width:250px;
}
</style>
If you need to add this CSS to a page where you have multiple lists, you can target a specific list/webpart by using F12 Developer Tools to find the web-part ID and add it before .ms-vh-div.
#WebPartWPQX .ms-vh-div[DisplayName='ColumnName'] 
Where X is the number/ID of the webpart.

Thursday, 15 November 2012

How to create InfoPath Forms Programmatically:

How to create InfoPath Forms Programmatically:
Following code will explain the way to create to create InfoPath forms programmatically.
Basic logic behind InfoPath form creation is just creating XML file to the form library. If we can add xml file to the form library then it will open in the form of InfoPath.

Syntax to create XML (InfoPath form):
StringBuilder SubFileTemplate = new StringBuilder();

SubFileTemplate.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><?mso-infoPathSolution name=\"urn:schemas-microsoft-com:office:infopath:TemplateName:-myXSD-2012-06-26T06-06-08\" solutionVersion=\"1.0.0.871\" productVersion=\"14.0.0.0\" PIVersion=\"1.0.0.0\" href=\"" + SPContext.Current.Site.Url + "/LibraryName/Forms/template.xsn\"?><?mso-application progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.3\"?><?mso-infoPath-file-attachment-present?><my:myFields xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-06-26T06:06:08\" xmlns:xd=\"http://schemas.microsoft.com/office/infopath/2003\" xml:lang=\"en-US\">");
SubFileTemplate.Append("<my:AccountNumber>" + valueforAcntNumber + "</my:AccountNumber>");
SubFileTemplate.Append("<my:Description>" + valueforDescription + "</my:Description>");
SubFileTemplate.Append("</my:myFields>");

string XmlPath = web.Url + "/FormLibraryName/" + “TestForm.xml”;
MemoryStream stream = new MemoryStream();
System.Text.UTF8Encoding myEncoding = new System.Text.UTF8Encoding();
byte[] myBuffer = myEncoding.GetBytes(SubFileTemplate.ToString());
stream.Write(myBuffer, 0, myBuffer.Length);
if (myBuffer != null && myBuffer.Length != 0)
 {
 web.Files.Add(XmlPath, myBuffer);//Adding XML file to library
}


Here create one stringbuilder object and add xml tags to the variable and then save content to the library in the format of xml file.
You can get correct XML format tags by using following way:
Publish your InfoPath form into the SharePoint site and create one dummy InfoPath form manually. Then you can download the same form and then open with Notepad. Then you will be able to see the correct xml tags for your form.
Screen shot for the same.

Once you create xml file to library programmatically then you will be able to open in InfoPath.

Problem in reading Excel data with OPENXML

Problem in reading Excel cells with OPENXML:
If the users are not using the correct template excel with the data then we will not be able to read the excel cells data. Here the issue will be, if we are trying to retrieve the 3rd cell value then it may through exception. Exception will throw because of cell not used.
The solution for this issue is, first we need to check whether particular is used or not. Then we need to try to read the data from that cell.
Normal syntax to read Excel Data:
using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);
                string Path = filePath;
                CultureInfo pt = CultureInfo.GetCultureInfo("fr-FR");
                dtMain.Locale = pt;


                using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(mem, true))
                {
                    WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
                    Sheet theSheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault();
                     //Where(s => s.Name == SheetName).FirstOrDefault();

                    if (theSheet == null)
                    {
                        ErrorMessage = "Invalid Sheet";
                        return ErrorMessage;
                    }

                    WorksheetPart worksheetPart = (WorksheetPart)(workbookPart.GetPartById(theSheet.Id));
                    Worksheet workSheet = worksheetPart.Worksheet;
                    SheetData sheetData = workSheet.GetFirstChild<SheetData>();
                    IEnumerable<Row> rows = sheetData.Descendants<Row>();

Boolean IsEndofFIle = false;


foreach (Row row in rows) //this will also include your header row...        
{
  for (int i = 0; i < 10; i++)
   {
    string ColName= GetColumnName(row.Descendants<Cell>().ElementAt(i).CellReference);
      if (!ColName.Equals("A") && !ColName.Equals("B") && !ColName.Equals("C")&& !ColName.Equals("D")&& !ColName.Equals("E"))
      {
         IsEndofFIle = true;
         break;
      }
     
      if (ColName.Contains("E"))
      {
       IVal = i;
       IMaxVal = i + 6;
   ColName = GetColumnName(row.Descendants<Cell>().ElementAt(i+1).CellReference);
    if (!ColName.Equals("F"))
    {
     IsDataValid = false;
     ErrorMessage += "Sheet contain invalid data</br>";
     break;
    }
break;
}
}

Above Syntax will get the column names like A,B,C,D,E,F….. So based on the column name we can retrieve the particular cell data.
Above code don’t have complete logic. It just have the syntax to read column name.

Solution for RunWithElevatedPrivileges not working

Solution for RunWithElevatedPrivileges not working:
When we use the ElevatedPrivileges then we need to use the following syntax
SPSecurity.RunWithElevatedPrivileges(delegate()
{

//Code to run with Elevated Previliges
}
Here Code will not run with elevated previliges, because we need to create web object with elevated previliges.
Following Syntax will work for this case:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using( SPSite site = new SPSite(SPContext.Current.Site.ID))
    {
       using (SPWeb web = site.OpenWeb())
        {
//Code to run with Elevated Previliges
 }
    }
 });

Here SPsite object is created with Elevated Privileges, so code will run as per the requirement.
The meaning of running code with elevated privileges means it runs with the application pool identity. By default it will be System Account.

If we need to run the code with Elevated Privileges but not with application pool identity then we should use the following syntax:
string ServiceAccount=”CompleteUserID”; // User id in variable
var user = SPContext.Current.Web.AllUsers[ServiceAccount]; //Creating User Token with user id
var superToken = user.UserToken;
using (var spSite = new SPSite(SPContext.Current.Web.Url, superToken))
 {
   using (SPWeb web3 = spSite.OpenWeb())
   {
          //Code to run with Elevated Previliges
   }
});

Here ServiceAccount is the string variable to assign userid. And we will use the same user token to create SPsite object.

Please let me know for any other clarification.

Friday, 9 November 2012

Welcome TO Sharepoint Solutions..

Coming Soon..................