October 12, 2009

Sharepoint: XML File Upload, File Level,Folder Level, Group Creation etc

---XML Doc---
<?xml version="1.0" encoding="utf-8" ?>
Products>
<Product Name="Product1">
<File>
<Title>1.txt</Title>
<Groups>One,Two,Three</Groups>
</File>
<File>
<Title>2.txt</Title>
<Title>3.txt</Title>
<Groups>Two</Groups>
</File>
<File>
<Title>4.txt</Title>
<Title>5.txt</Title>
<Groups>Three</Groups>
</File>
</Product>
<Product Name="Product2">
<File>
<Title>6.txt</Title>
<Title>7.txt</Title>
<Groups>Four,Six</Groups>
</File>
</Product>
<Product Name="Product3">
<File>
<Title>8.txt</Title>
<Title>9.txt</Title>
<Groups>Five</Groups>
</File>
</Product>
<Product Name="Product4">
<File>
<Title>10.txt</Title>
<Title>11.txt</Title>
<Groups>Five,Six</Groups>
</File>
</Product>
</Products>
---Button Click---
DirectoryInfo Dir = new DirectoryInfo(@"FolderURL");
FileInfo[] CxiFiles = Dir.GetFiles();
foreach (FileInfo fi in CxiFiles)
{
XmlTextReader Reader = new XmlTextReader(Dir.ToString() + fi.ToString());
Reader.WhitespaceHandling = WhitespaceHandling.None;
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Reader);
Reader.Close();
XmlNode RootNode = XmlDoc.DocumentElement;
Children(RootNode, 1);
lines = lines.Replace("|", "\r\n");
}

------Main Logic---
private void Children(XmlNode xnod, Int32 intLevel)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
XmlNode xnodWorking;
XmlNode node1;
string ListName = "Docs";
SPSite siteCollection = new SPSite(@"URL");
SPWeb siteWeb = siteCollection.RootWeb;
SPListCollection spList = siteWeb.Lists;
SPFolder spFolder = null;
if (xnod.Name.Substring(0, 4) == "Prod")
{
if (siteWeb.GetFolder(ListName + "/" + xnod.Attributes[0].Value).Exists == true)
{
spFolder = siteWeb.Folders[ListName].SubFolders[xnod.Attributes[0].Value];
SPListItem item = spFolder.Item;
item["DocType"] = "Folder";
item["Product Name"] = spFolder.Name;
item.Update();
}
else
{
spFolder = siteWeb.Folders[ListName].SubFolders.Add(xnod.Attributes[0].Value);
SPListItem item = spFolder.Item;
item["DocType"] = "Folder";
item["Product Name"] = spFolder.Name;
item.Update();
}
}
if (xnod.Name.Substring(0, 4) == "File")
{
node1 = xnod.FirstChild;
while (node1 != null)
{
spFolder = siteWeb.Folders[ListName].SubFolders[xnod.ParentNode.Attributes[0].Value];
spFolder.Item.BreakRoleInheritance(false);
siteWeb.AllowUnsafeUpdates = true;
if (node1.Name.Substring(0, 1) == "T")
{
string FilePath = @"C:\Source Folder\" + node1.InnerText;
string FileName = System.IO.Path.GetFileName(FilePath);
string DocType = node1.Attributes["DocType"].Value;
System.IO.FileStream Strm = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader reader = new System.IO.BinaryReader(Strm);
Byte[] filecontents = reader.ReadBytes(Convert.ToInt32(Strm.Length));
reader.Close();
Strm.Close();
spFolder.Files.Add(System.IO.Path.GetFileName(FilePath), filecontents, true);
SPListItem item = file.Item;
item["DocType"] = DocType;
item["Product Name"] = spFolder.Name;
item.Update();
node1 = node1.NextSibling;
}
else
if (node1.Name.Substring(0, 1) == "G")
{
//string Grps = xnod.FirstChild.NextSibling.InnerText;
string Grps = node1.InnerText;
string[] grps = Grps.Split(',');
foreach (string g in grps)
{
try
{
if (siteWeb.SiteGroups[g] != null)
{
SPGroup group2 = siteWeb.SiteGroups[g];
SPRoleDefinition RoleDefinition = siteWeb.RoleDefinitions["Read"];
SPRoleAssignment RoleAssignment = new SPRoleAssignment((SPPrincipal)siteWeb.SiteGroups[g]);
RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
siteWeb.RoleAssignments.Add(RoleAssignment);
spFolder.Item.RoleAssignments.Add(RoleAssignment);
SPList lst = siteWeb.Lists[ListName];
SPListItemCollection items = lst.Items;
foreach (SPListItem item in items)
{
foreach (XmlNode x in node1.ParentNode.ChildNodes)
{
if (item.Name == x.InnerText)
{
item.BreakRoleInheritance(false);
item.RoleAssignments.Add(RoleAssignment);
item.Update();
}
}
}
}
}
catch (SPException ex)
{
SPMember member = siteWeb.SiteUsers["admembershipprovider:krish.banerjee"];
SPUser usr = siteWeb.SiteUsers["admembershipprovider:krish.banerjee"];
siteWeb.SiteGroups.Add(g, member, usr, g);
AddToValidDomains(g);
SPGroup group2 = siteWeb.SiteGroups[g];
SPRoleDefinition RoleDefinition = siteWeb.RoleDefinitions["Read"];
SPRoleAssignment RoleAssignment = new SPRoleAssignment((SPPrincipal)siteWeb.SiteGroups[g]);
RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
siteWeb.RoleAssignments.Add(RoleAssignment);
spFolder.Item.RoleAssignments.Add(RoleAssignment);
SPList lst = siteWeb.Lists[ListName];
SPListItemCollection items = lst.Items;
foreach (SPListItem item in items)
{
if (item.Name == node1.PreviousSibling.InnerText)
{
item.BreakRoleInheritance(false);
item.RoleAssignments.Add(RoleAssignment);
item.Update();
}
}
}
}
spFolder.Update();
node1 = node1.NextSibling;
}
}
}
if (xnod.NodeType == XmlNodeType.Element)
{
if (xnod.HasChildNodes)
{
xnodWorking = xnod.FirstChild;
while (xnodWorking != null)
{
Children(xnodWorking, intLevel + 1);
xnodWorking = xnodWorking.NextSibling;
}
}
}
lblError.Text = "Success";
});
}

No comments:

SonarQube with Jenkins Setup using Docker Images

https://funnelgarden.com/sonarqube-jenkins-docker/  https://medium.com/@hakdogan/an-end-to-end-tutorial-to-continuous-integration-and-con...