예제 #1
0
        private int getResourceWssId(Guid RES_GUID)
        {
            try
            {
                WebSvcResource.ResourceDataSet rDs = new WebSvcResource.ResourceDataSet();
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    rDs = psiResource.ReadResource(RES_GUID);
                });

                if (rDs.Resources.Count > 0)
                {
                    if (rDs.Resources[0].RES_IS_WINDOWS_USER)
                    {
                        string username = rDs.Resources[0].WRES_ACCOUNT;
                        try
                        {
                            return(myWebToPublish.AllUsers[username].ID);
                        }
                        catch
                        {
                            return(0);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                myLog.WriteEntry("Error in getResourceWssId(): " + ex.Message + ex.StackTrace, EventLogEntryType.Error, 330);
            }
            return(0);
        }
예제 #2
0
        private string getResourceUsername(string url, Guid RES_GUID)
        {
            try
            {
                WebSvcResource.Resource pResource = new WebSvcResource.Resource();
                pResource.Url = url + "/_vti_bin/psi/resource.asmx";
                pResource.UseDefaultCredentials = true;
                WebSvcResource.ResourceDataSet rDs = new WebSvcResource.ResourceDataSet();

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    rDs = pResource.ReadResource(RES_GUID);
                });

                if (rDs.Resources.Count > 0)
                {
                    if (rDs.Resources[0].RES_IS_WINDOWS_USER)
                    {
                        return(rDs.Resources[0].WRES_ACCOUNT);
                    }
                }
            }
            catch (Exception ex)
            {
                string logEntry = "Error: " + ex.Message;
                ErrorTrap(3003, logEntry);
            }
            return("");
        }
        public void processResource(SPWeb web, Guid resGuid, SPWeb resWeb)
        {
            WebSvcResource.ResourceDataSet rds = pResource.ReadResource(resGuid);


            try
            {
                SPUser  user  = null;
                SPGroup group = null;

                if (!rds.Resources[0].IsWRES_ACCOUNTNull())
                {
                    try
                    {
                        user = resWeb.SiteUsers[rds.Resources[0].WRES_ACCOUNT];
                    }
                    catch { }
                    if (user == null)
                    {
                        string email = "";
                        if (!rds.Resources[0].IsWRES_EMAILNull())
                        {
                            email = rds.Resources[0].WRES_EMAIL;
                        }

                        resWeb.SiteUsers.Add(rds.Resources[0].WRES_ACCOUNT, email, rds.Resources[0].RES_NAME, "");
                        user = resWeb.SiteUsers[rds.Resources[0].WRES_ACCOUNT];
                    }
                }
                else
                {
                    try
                    {
                        group = resWeb.SiteGroups[rds.Resources[0].RES_NAME];
                    }
                    catch { }
                }
                SPList list = resWeb.Lists["Resources"];

                if (!list.Fields.ContainsField("ResUid"))
                {
                    SPFieldText fldResUid = (SPFieldText)list.Fields.CreateNewField(SPFieldType.Text.ToString(), "ResUid");
                    fldResUid.Hidden   = true;
                    fldResUid.Required = false;
                    list.Fields.Add(fldResUid);
                    list.Update();
                }

                DataRow [] drFields = rds.ResourceCustomFields.Select("RES_UID='" + rds.Resources[0].RES_UID + "'");
                foreach (DataRow drField in drFields)
                {
                    try
                    {
                        string fname = "ENT" + drField["MD_PROP_ID"];
                        if (!list.Fields.ContainsField(fname))
                        {
                            addField(list, drField["MD_PROP_ID"].ToString(), drField["FIELD_TYPE_ENUM"].ToString());
                        }
                    }
                    catch { }
                }

                SPQuery query = new SPQuery();
                if (user != null)
                {
                    query.Query = "<Where><Eq><FieldRef Name='SharePointAccount'/><Value Type='User'>" + user.Name + "</Value></Eq></Where>";
                }
                else if (group != null)
                {
                    query.Query = "<Where><Eq><FieldRef Name='SharePointAccount'/><Value Type='User'>" + group.Name + "</Value></Eq></Where>";
                }
                else
                {
                    query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + rds.Resources[0].RES_NAME + "</Value></Eq></Where>";
                }

                SPListItemCollection lic = list.GetItems(query);

                SPListItem li = null;

                if (lic.Count > 0)
                {
                    li = lic[0];
                }
                else
                {
                    li = list.Items.Add();
                }

                if (user != null)
                {
                    li["SharePointAccount"] = new SPFieldUserValue(resWeb, user.ID, user.Name);
                }

                if (group != null)
                {
                    li["SharePointAccount"] = new SPFieldUserValue(resWeb, group.ID, group.Name);
                }

                li["ResUid"] = resGuid.ToString();

                if (li != null)
                {
                    string name      = rds.Resources[0].RES_NAME;
                    string firstname = "";
                    try
                    {
                        firstname = name.Split(' ')[0];
                    }
                    catch { }
                    string lastname = "";
                    try
                    {
                        lastname = name.Substring(firstname.Length + 1);
                    }
                    catch { }

                    li["Title"]     = rds.Resources[0].RES_NAME;
                    li["FirstName"] = firstname;
                    li["LastName"]  = lastname;
                    if (!rds.Resources[0].IsWRES_EMAILNull())
                    {
                        li["Email"] = rds.Resources[0].WRES_EMAIL;
                    }

                    if (!rds.Resources[0].IsRES_TIMESHEET_MGR_UIDNull())
                    {
                        if (rds.Resources[0].RES_TIMESHEET_MGR_UID != rds.Resources[0].RES_UID)
                        {
                            WebSvcResource.ResourceDataSet tsRds = pResource.ReadResource(rds.Resources[0].RES_TIMESHEET_MGR_UID);
                            if (tsRds.Resources.Count > 0)
                            {
                                try
                                {
                                    SPUser tuser = null;

                                    try
                                    {
                                        tuser = resWeb.SiteUsers[tsRds.Resources[0].WRES_ACCOUNT];
                                    }
                                    catch { }
                                    if (tuser == null)
                                    {
                                        resWeb.SiteUsers.Add(tsRds.Resources[0].WRES_ACCOUNT, tsRds.Resources[0].WRES_EMAIL, tsRds.Resources[0].RES_NAME, "");
                                        tuser = resWeb.SiteUsers[tsRds.Resources[0].WRES_ACCOUNT];
                                    }

                                    li["TimesheetManager"] = new SPFieldUserValue(resWeb, tuser.ID, tuser.Name);
                                }
                                catch { }
                            }
                        }
                    }

                    foreach (DataRow drField in drFields)
                    {
                        try
                        {
                            string fname = "ENT" + drField["MD_PROP_ID"];
                            if (list.Fields.ContainsField(fname))
                            {
                                string fVal = getfieldvalue(drField);
                                li[list.Fields.GetFieldByInternalName("ENT" + drField["MD_PROP_ID"]).Id] = fVal;
                            }
                        }
                        catch { }
                    }

                    li.SystemUpdate();
                }
            }
            catch { }
        }