If somebody like the post and its helpful in your work then, add comments.

Monday, March 23, 2009

Automatically add new row to repeating control

The code behind the Add New Record button is very simply
The code is: (c#)

CurrentView.ExecuteAction(ActionType.XCollectionInsert, "group2_1");

Issue:“The feature name WebPart1 already exists in SharePoint. You need to rename the feature before solution deployment can succeed.”

This error happens even if you given a different name for your web part file and web part class file. This is because behind the scenes Visual Studio stores the solution name as Webpart1 even if you have given a different name to your webpart. To overcome this VS 2008 behavior, following changes have to be made to the webpart feature files
1. Change the name of webpart folder, webpart xml and webpart files from Webpart1 to the name of your web part for e.g. WP1. So in effect the folder WebPart1 will become WP1, the file WebPart1.webpart will become WP1.webpart and the file WebPart1.xml will become WP1.xml.
2. Select the Show All Files option from VS 2008 Solution Explorer tool bar.
3. Three additional folders namely bin, obj, pkg will be displayed in grayed out style.
4. Expand the pkg folder. Pkg folder will have a sub folder named Webpart1 and two XML files manifest.xml and solution.xml.
5. Rename Webpart1 folder name to your webpart for e.g. WP1
6. Edit solution.xml and change the name of feature from Webpart1 to your webpart name

Error Handling in the InfoPath in VSTA

All the error which occures on design time are be collected in the FormErrorCollection

To get all the design time error like validation, schema error the write bellow code

FormErrorCollection MyError = this.Errors;

This MyError will have all the error which occured in the form.


MyError have all the details of error of all the control. Loop into this and get all the detials.

foreach (FormError AllError in MyError)
{
//write message box code
}

Then show the details in the message box or any other control in the form

string errorMessage = string.Empty;

//write message box code
errorMessage = errorMessage + AllError.Site.LocalName + " : " + AllError.Message + System.Environment.NewLine;


and message box

if (MyError.Count != 0)
{
MessageBox.Show(errorMessage);
}

Full Code :

FormErrorCollection MyError = this.Errors;
string errorMessage = string.Empty;
foreach (FormError AllError in MyError)
{
errorMessage = errorMessage + AllError.Site.LocalName + " : " + AllError.Message + System.Environment.NewLine;

}

if (MyError.Count != 0)
{
MessageBox.Show(errorMessage);
}

Sunday, March 15, 2009

Inserting Image by Open XML API's

// To add an image part to a package.
public static void AddImagePart(string document, string fileName)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
string imagePartid = mainPart.GetIdOfPart(imagePart);

XmlDocument mainPartXML = new XmlDocument();
mainPartXML.Load(mainPart.GetStream());
XmlNode MainPartNode = mainPartXML.CreateNode(XmlNodeType.Element, "w:p", mainPartXML.DocumentElement.NamespaceURI);

XmlNamespaceManager XNameSpceMgr = new XmlNamespaceManager(mainPartXML.NameTable);
XNameSpceMgr.AddNamespace("ns", mainPartXML.DocumentElement.NamespaceURI);
XNameSpceMgr.AddNamespace("w", mainPartXML.DocumentElement.NamespaceURI);

// Load the drawing template into an XML document.
XmlDocument drawingXml = new XmlDocument();
drawingXml.Load(@"C:\Documents and Settings\Administrator\Desktop\Image\drawingTemplate.xml");
//Load the drawing template into an XML document and pass the reference ID parameter.
drawingXml.LoadXml(string.Format(drawingXml.InnerXml, imagePartid));
XmlNamespaceManager XNameSpceImageMgr = new XmlNamespaceManager(drawingXml.NameTable);
XNameSpceImageMgr.AddNamespace("w", drawingXml.DocumentElement.NamespaceURI);
// Load the drawing template into an XML document and pass the reference ID parameter.
drawingXml.LoadXml(string.Format(drawingXml.InnerXml, imagePartid));
XmlNode XNode = mainPartXML.SelectSingleNode("//ns:body/ns:p", XNameSpceMgr);
XmlNode XNodeParent = mainPartXML.SelectSingleNode("//ns:body", XNameSpceMgr);
MainPartNode.InnerXml = drawingXml.FirstChild.InnerXml.ToString();
XNodeParent.AppendChild(MainPartNode);
mainPartXML.Save(mainPart.GetStream());
wordDoc.Close();
}
}

drawingTemplate.xml