using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication_SetFieldOptional
{
class Program
{
static void Main(string[] args)
{
//string strLinkField = "Site,Link,Link";
//string[] strArrlist=strListName.Split(',');
// string[] strArrLinkField=strLinkField.Split(',');
// SPUser userAdm = tempCurrentWeb.AllUsers[@"\site-admin"];
// SPUserToken tokenPO = userAdm.UserToken;
using (SPSite site = new SPSite(strSiteUrl))
{
using(SPWeb web= site.OpenWeb())
{
foreach (SPWeb web1 in web.Webs)
{
// SPList spls = web.Lists[strListName];
//foreach (SPListItem item in spls.Items)
{
// string strLink=item[spls.Fields[strArrLinkField[i]].InternalName]!= null ?item[spls.Fields[strArrLinkField[i]].InternalName].ToString().Split(',')[0]:"";
// if (strLink != "")
{
using (SPSite Subsite = new SPSite(web1.Url))
{
using (SPWeb newSubWeb = openWebPermissions(Subsite.OpenWeb()))
{
SPUser userAdm = newSubWeb.AllUsers[@"iqubes\iqs-site-admin"];
SPUserToken tokenPO = userAdm.UserToken;
#region code to make the "Resource" field of "Resources" list hidden
try
{
HideField(newSubWeb, tokenPO, "Resources", "Resource");
}
catch
{
;
}
#endregion
#region code to make the "Contact name" field of "Contacts" list optional
try
{
//SPUser userAdm = tempCurrentWeb.AllUsers[Utility.AdminAccount];
//SPUserToken tokenPO = userAdm.UserToken;
SetFieldOptional(newSubWeb, tokenPO, "Contacts", "Contact name");
}
catch
{
;
}
#endregion
#region code to make the "Title" field of "Documents" list optional
try
{
//SPUser userAdm = tempCurrentWeb.AllUsers[Utility.AdminAccount];
//SPUserToken tokenPO = userAdm.UserToken;
SetFieldOptional(newSubWeb, tokenPO, "Documents", "Title");
}
catch
{
;
}
#endregion
}
}
}
}
}
}
}
}
public static void SetFieldOptional(SPWeb newSubWeb, SPUserToken tokenPO, string Listname, string Fieldname)
{
try
{
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
// using (SPSite site = new SPSite(newSubWeb.Url, tokenPO))
// {
// using (SPWeb web = site.OpenWeb())
// {
newSubWeb.AllowUnsafeUpdates = true;
SPList SubList = newSubWeb.Lists[Listname];
SPContentType SubContentType = SubList.ContentTypes[0];
SPField spSubField = SubContentType.Fields.GetField(Fieldname);
SPFieldLinkCollection linksFlds = SubContentType.FieldLinks;
var Curlink = linksFlds[spSubField.Id];
if (Curlink != null && Curlink.Required == true)
{
Curlink.Required = false;
SubContentType.Update(false);
}
newSubWeb.AllowUnsafeUpdates = false;
// }
// }
//});
}
catch
{
}
}
public static void HideField(SPWeb newSubWeb, SPUserToken tokenPO, string Listname, string Fieldname)
{
try
{
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
// using (SPSite site = new SPSite(newSubWeb.Url, tokenPO))
// {
// using (SPWeb web = site.OpenWeb())
// {
newSubWeb.AllowUnsafeUpdates = true;
SPList SubList = newSubWeb.Lists[Listname];
SPContentType SubContentType = SubList.ContentTypes[0];
SPField spSubField = SubContentType.Fields.GetField(Fieldname);
SPFieldLinkCollection linksFlds = SubContentType.FieldLinks;
var Curlink = linksFlds[spSubField.Id];
if (Curlink != null && Curlink.Required == true)
{
Curlink.Hidden = true;
SubContentType.Update(false);
}
newSubWeb.AllowUnsafeUpdates = false;
// }
// }
//});
}
catch
{
}
}
private const string _adminUser = "-site-admin";
private static SPWeb openWebPermissions(SPWeb currentWeb)
{
try
{
SPUser user = currentWeb.AllUsers[_adminUser];
SPUserToken token = user.UserToken;
SPSite site = new SPSite(currentWeb.Url, token);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
return web;
}
catch (Exception ex)
{
}
return currentWeb;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication_SetFieldOptional
{
class Program
{
static void Main(string[] args)
{
//string strLinkField = "Site,Link,Link";
//string[] strArrlist=strListName.Split(',');
// string[] strArrLinkField=strLinkField.Split(',');
// SPUser userAdm = tempCurrentWeb.AllUsers[@"\site-admin"];
// SPUserToken tokenPO = userAdm.UserToken;
using (SPSite site = new SPSite(strSiteUrl))
{
using(SPWeb web= site.OpenWeb())
{
foreach (SPWeb web1 in web.Webs)
{
// SPList spls = web.Lists[strListName];
//foreach (SPListItem item in spls.Items)
{
// string strLink=item[spls.Fields[strArrLinkField[i]].InternalName]!= null ?item[spls.Fields[strArrLinkField[i]].InternalName].ToString().Split(',')[0]:"";
// if (strLink != "")
{
using (SPSite Subsite = new SPSite(web1.Url))
{
using (SPWeb newSubWeb = openWebPermissions(Subsite.OpenWeb()))
{
SPUser userAdm = newSubWeb.AllUsers[@"iqubes\iqs-site-admin"];
SPUserToken tokenPO = userAdm.UserToken;
#region code to make the "Resource" field of "Resources" list hidden
try
{
HideField(newSubWeb, tokenPO, "Resources", "Resource");
}
catch
{
;
}
#endregion
#region code to make the "Contact name" field of "Contacts" list optional
try
{
//SPUser userAdm = tempCurrentWeb.AllUsers[Utility.AdminAccount];
//SPUserToken tokenPO = userAdm.UserToken;
SetFieldOptional(newSubWeb, tokenPO, "Contacts", "Contact name");
}
catch
{
;
}
#endregion
#region code to make the "Title" field of "Documents" list optional
try
{
//SPUser userAdm = tempCurrentWeb.AllUsers[Utility.AdminAccount];
//SPUserToken tokenPO = userAdm.UserToken;
SetFieldOptional(newSubWeb, tokenPO, "Documents", "Title");
}
catch
{
;
}
#endregion
}
}
}
}
}
}
}
}
public static void SetFieldOptional(SPWeb newSubWeb, SPUserToken tokenPO, string Listname, string Fieldname)
{
try
{
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
// using (SPSite site = new SPSite(newSubWeb.Url, tokenPO))
// {
// using (SPWeb web = site.OpenWeb())
// {
newSubWeb.AllowUnsafeUpdates = true;
SPList SubList = newSubWeb.Lists[Listname];
SPContentType SubContentType = SubList.ContentTypes[0];
SPField spSubField = SubContentType.Fields.GetField(Fieldname);
SPFieldLinkCollection linksFlds = SubContentType.FieldLinks;
var Curlink = linksFlds[spSubField.Id];
if (Curlink != null && Curlink.Required == true)
{
Curlink.Required = false;
SubContentType.Update(false);
}
newSubWeb.AllowUnsafeUpdates = false;
// }
// }
//});
}
catch
{
}
}
public static void HideField(SPWeb newSubWeb, SPUserToken tokenPO, string Listname, string Fieldname)
{
try
{
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
// using (SPSite site = new SPSite(newSubWeb.Url, tokenPO))
// {
// using (SPWeb web = site.OpenWeb())
// {
newSubWeb.AllowUnsafeUpdates = true;
SPList SubList = newSubWeb.Lists[Listname];
SPContentType SubContentType = SubList.ContentTypes[0];
SPField spSubField = SubContentType.Fields.GetField(Fieldname);
SPFieldLinkCollection linksFlds = SubContentType.FieldLinks;
var Curlink = linksFlds[spSubField.Id];
if (Curlink != null && Curlink.Required == true)
{
Curlink.Hidden = true;
SubContentType.Update(false);
}
newSubWeb.AllowUnsafeUpdates = false;
// }
// }
//});
}
catch
{
}
}
private const string _adminUser = "-site-admin";
private static SPWeb openWebPermissions(SPWeb currentWeb)
{
try
{
SPUser user = currentWeb.AllUsers[_adminUser];
SPUserToken token = user.UserToken;
SPSite site = new SPSite(currentWeb.Url, token);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
return web;
}
catch (Exception ex)
{
}
return currentWeb;
}
}
}
No comments:
Post a Comment